簡體   English   中英

如何解析包含多個對象的JSON響應

[英]How to parse JSON response that includes multiple objects

我正在調用的REST API在單個JSON響應中返回多個對象。 使用Python 2.7,我試圖解析此響應,以便可以為響應中的ALL對象打印其中一些值。

從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"
  }
]

響應只是一個對象數組。 您需要遍歷此數組並為一個對象打印要打印的鍵。

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

遍歷響應列表以獲取所需鍵的值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM