【问题标题】:How to parse JSON response that includes multiple objects如何解析包含多个对象的 JSON 响应
【发布时间】: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"
  }
]

【问题讨论】:

    标签: python json rest api jira


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • “Active”键返回布尔值而不是字符串。这导致NameError: name 'true' is not defined。有没有一种简单的方法可以在不手动调整数据的情况下修复此错误?
      • 响应是通过直接从 Python 脚本向 API 发出的 GET 请求接收的。我检查了 API 文档,默认情况下它是一个布尔值。
      • @Falco 不用担心,只需将响应转换为字符串,然后按照我在第二次编辑中显示的内容进行操作!
      • 一个问题,我是否需要手动为JSON响应中的每个新对象添加“”?
      • @Falco 不,只需将您收到的回复转换为str
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多