如何解析包含多个对象的JSON响应

时间:2019-04-08 14:08:14

标签: python json rest api jira

我正在调用的REST API在单个JSON响应中返回多个对象。我正在使用Python 2.7解析此响应,以便可以为响应中的所有对象打印其中一些值。

从REST API响应中给出的所有对象中提取数据的最佳方法是什么?

我想要一个列表,其中包含JSON响应中每个对象的“键”,“名称”和“ 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"
  }
]

2 个答案:

答案 0 :(得分:2)

响应只是一个对象数组。您需要遍历此数组并为一个对象打印要打印的键。

resp = ...
for a in resp:
    print a.get(key), ";", a.get('name'), ";", a.get('emailAddress')

答案 1 :(得分:0)

遍历响应列表以获取所需键的值:

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'))

输出

keyOne nameOne mailOne
keyTwo nameTwo mailTwo

编辑

如果要在元组列表中使用它们:

print([(elem.get('key', None), elem.get('name', None), elem.get('emailAddress', None)) for elem in j_res])

输出

[('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])

输出

[('keyOne', 'nameOne', 'mailOne'), ('keyTwo', 'nameTwo', 'mailTwo')]