我正在使用请求库,Python 3.6和SMS平台的Swagger API。我对如何从此JSON层次结构中提取特定的key:value感到有些困惑。
这是API文档的URL:api docs
我正在尝试根据ID向端点查询特定的电话号码。
这是要查询的URL:https://api.kenect.com/v1/conversations/ \ * \ * ID \ * \ *这是JSON层次结构,因此我想使用“ phoneNumbers”键和“数字的值:
{
"archiveDate": 1514801105185,
"assignedTeamId": 0,
"assignedTeamName": "string",
"assignedUser": {
"departmentId": 1234,
"firstName": "John",
"id": 1234,
"inactiveDate": 1514801105185,
"includeSignature": true,
"lastName": "Doe",
"messageSignature": "Best, John Doe",
"username": "jdoe@mymail.com"
},
"contact": {
"createdDate": 1514801105185,
"emailAddress": "contact@email.com",
"externalId": "8adf-7865",
"firstActiveDate": 1514801105185,
"firstName": "Jane",
"groups": [
{
"createdDate": 1514801105185,
"id": 1234,
"locationId": 1234,
"name": "the inner circle",
"updatedDate": 1514801105185
}
],
"id": 1234,
"inactiveDate": 1514801105185,
"lastContacted": 1514801105185,
"lastName": "Doe",
"locationId": 12345,
"note": "string",
"phoneNumbers": [
{
"city": "Pleasant Grove",
"country": "US",
"createdDate": 1514801105185,
"id": 1234,
"messageOptInDate": 1514801105185,
"messageStopDate": 1514801105185,
"number": "+18015552671",
"primary": true,
"sentOptInMessageDate": 1514801105185,
"smsCapable": true,
"state": "UT",
"status": true,
"type": "CELL",
"updatedDate": 1514801105185,
"zipCode": 84062
}
],
"reviewRequestSent": 1514801105185,
"source": "LIGHTSPEED",
"updatedDate": 1514801105185
},
"createdDate": 1514801105185,
"dataAssigned": 0,
"id": 1234,
"lastMessageType": "FACEBOOK",
"lastSent": 1514801105185,
"locationId": 0,
"messages": [
{
"assignedUserId": 0,
"attachments": [
{
"contentType": "string",
"createdDate": 1514801105185,
"friendlyName": "string",
"id": 1234,
"md5": "string",
"size": 16384,
"thumbnailBase64": "string",
"updatedDate": 1514801105185
}
],
"body": "string",
"contactId": 0,
"conversationId": 0,
"error": "string",
"id": 1234,
"kind": "string",
"locationId": 0,
"outgoing": true,
"readDate": 0,
"respondedDate": 0,
"reviewRequest": true,
"sentDate": 0,
"status": "string",
"type": "FACEBOOK"
}
],
"newConversation": true,
"read": true,
"responded": true,
"snippet": "string",
"subject": "string",
"updatedDate": 1514801105185
}
最后,这是我正在尝试的代码。我曾使用类似的功能从同一端点获取消息正文,但不适用于电话号码。
url='https://api.kenect.com/v1/conversations/'
h = {'accept': recContentType,
'content-type': recContentType,
'x-api-token': apiToken,
'x-api-Key': apiKey}
p={'id': id}
response=requests.get(url + id, headers=h, params=p).json()
for item in response['phoneNumber']:
pnum = item['number']
print(pnum)
我在这里做错了什么?
问题来源:stackoverflow
这是您获取号码所需的路径:
.contact.phoneNumbers[0].number
因此,您将需要以下内容:
number = response['contact']['phoneNumbers'][0]['number']
print(number)
或编辑您的代码:
url='https://api.kenect.com/v1/conversations/'
h = {'accept': recContentType,
'content-type': recContentType,
'x-api-token': apiToken,
'x-api-Key': apiKey}
p={'id': id}
response=requests.get(url + id, headers=h, params=p).json()
for item in response['contact']['phoneNumbers']:
pnum = item['number']
print(pnum)
根据以下注释,如果json中的字段丢失,则上面的代码可能会导致错误。例如,如果“响应”没有“联系”,那么就会出现问题。
请考虑在上面的代码中添加一些防弹措施,如下所示:
url='https://api.kenect.com/v1/conversations/'
h = {'accept': recContentType,
'content-type': recContentType,
'x-api-token': apiToken,
'x-api-Key': apiKey}
p={'id': id}
response=requests.get(url + id, headers=h, params=p).json()
if response:
if 'contact' in response:
if 'phoneNumbers' in response['contact']:
for item in response['contact']['phoneNumbers']:
pnum = item['number']
print(pnum)
我认为在新版本的代码中有足够的检查方法来避免您在下面的注释中描述的错误。现在,我们仅在响应不为空且包含“联系人”键(而该键又必须包含“ phoneNumbers”键)时处理响应。当phoneNumber不包含数字,但是不会产生错误时,仍有机会打印None。请注意,pnum可能为None。
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。