【发布时间】:2019-08-29 17:13:35
【问题描述】:
我正在调用的 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"
}
]
【问题讨论】: