我正在调用的REST API在单个JSON响应中返回多个对象。使用Python 2.7我试图解析这个响应,以便我可以为响应中的所有对象打印一些这些值。
从REST API的响应中提供的所有对象中提取此数据的最佳方法是什么?
我想要一个列表,其中包含JSON响应中每个对象的“key”,“name”和“emailAddress”。
这就是我为响应中的单个对象做的事情:
>>>> a = json.loads(response)
>>> print a.get(key), ";", a.get('name'), ";", a.get('emailAddress')
keyOne ; nameOne ; mailOne
对于多个对象,我希望每个对象都有密钥;名称;显示在新行上的电子邮件。
响应数据的结构如下:
[
{
"self": "https://example1.com",
"key": "keyOne",
"name": "nameOne",
"emailAddress": "mailOne",
"avatarUrls": {
"48x48": "https://test.com/secure/useravatar?avatarId=1",
"24x24": "https://test.com/secure/useravatar?size=small&avatarId=1",
"16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=1",
"32x32": "https://test.com/secure/useravatar?size=medium&avatarId=1"
},
"displayName": "displayNameOne",
"active": true,
"timeZone": "Europe",
"locale": "en_UK"
},
{
"self": "https://example2.com",
"key": "keyTwo",
"name": "nameTwo",
"emailAddress": "mailTwo",
"avatarUrls": {
"48x48": "https://test.com/secure/useravatar?avatarId=2",
"24x24": "https://test.com/secure/useravatar?size=small&avatarId=2",
"16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=2",
"32x32": "https://test.com/secure/useravatar?size=medium&avatarId=2"
},
"displayName": "displayNameTwo",
"active": false,
"timeZone": "Europe",
"locale": "en_US"
}
]
迭代响应列表以获取所需键的值:
j_res = [
{
"self": "https://example1.com",
# rest of the response here
"timeZone": "Europe",
"locale": "en_US"
}
]
for elem in j_res:
print(elem.get('key', "Key does not exist"), elem.get('name', 'Name does not exist'), elem.get('emailAddress', 'emailAddress does not exist'))
OUTPUT:
keyOne nameOne mailOne
keyTwo nameTwo mailTwo
编辑:
如果你想要它们在元组列表中:
print([(elem.get('key', None), elem.get('name', None), elem.get('emailAddress', None)) for elem in j_res])
OUTPUT:
[('keyOne', 'nameOne', 'mailOne'), ('keyTwo', 'nameTwo', 'mailTwo')]
编辑2:
由于响应返回布尔值(true
/ false
)而不是字符串,因此解决方法可能是将响应转换为字符串,然后用字符串替换无效的布尔值,然后迭代列表:
j_res = '''[
{
"self": "https://example1.com",
"key": "keyOne",
"name": "nameOne",
"emailAddress": "mailOne",
"avatarUrls": {
"48x48": "https://test.com/secure/useravatar?avatarId=1",
"24x24": "https://test.com/secure/useravatar?size=small&avatarId=1",
"16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=1",
"32x32": "https://test.com/secure/useravatar?size=medium&avatarId=1"
},
"displayName": "displayNameOne",
"active": true, # notice this
"timeZone": "Europe",
"locale": "en_UK"
},
{
"self": "https://example2.com",
"key": "keyTwo",
"name": "nameTwo",
"emailAddress": "mailTwo",
"avatarUrls": {
"48x48": "https://test.com/secure/useravatar?avatarId=2",
"24x24": "https://test.com/secure/useravatar?size=small&avatarId=2",
"16x16": "https://test.com/secure/useravatar?size=xsmall&avatarId=2",
"32x32": "https://test.com/secure/useravatar?size=medium&avatarId=2"
},
"displayName": "displayNameTwo",
"active": false, # notice this
"timeZone": "Europe",
"locale": "en_US"
}
]'''
from ast import literal_eval
res = literal_eval(j_res.replace("true","'true'").replace("false", "'false'"))
print([(elem.get('key', None), elem.get('name', None), elem.get('emailAddress', None)) for elem in res])
OUTPUT:
[('keyOne', 'nameOne', 'mailOne'), ('keyTwo', 'nameTwo', 'mailTwo')]
响应只是一个对象数组。您需要迭代此数组并打印要为一个对象打印的键。
resp = ...
for a in resp:
print a.get(key), ";", a.get('name'), ";", a.get('emailAddress')