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

7bsow1i6  于 6个月前  发布在  其他
关注(0)|答案(3)|浏览(93)

How to parse JSON response that includes multiple objects参考上面的链接,我在响应中得到多个JSON对象,但与上面的链接中的响应不同(它接收多个JSON的单个数组),我得到多个JSON,两个JSON之间没有方括号和逗号
响应数据的结构如下:

{
"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"
}

字符串
我尝试循环通过REST API响应,想到将响应括在方括号内,但都失败了。
Parsing response with multiple json objects我检查了上面的链接,但这里也有,但他们在单行中有一个JSON元素,这与我的情况不同。如何使用Python解决这个任务,请帮助。

a0zr77ik

a0zr77ik1#

您的数据包含多个JSON对象。可以通过检测仅包含}的行和仅包含{的相邻行来分隔它们
因此:

data = '''
{
"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"
}
'''
from json import loads, dumps

jlist = []
k = 0

for i, line in enumerate(lines := data.splitlines()):
    if i and line == "{" and lines[i-1] == "}":
        jlist.append(loads("".join(lines[k:i])))
        k = i

jlist.append(loads("".join(lines[k:])))

for j in jlist:
    print(dumps(j, indent=2))

字符串

kwvwclae

kwvwclae2#

奇怪的是,像API这样的东西用看起来像JSON但不是有效JSON的东西来响应。
无论如何,如果JSON结构遵循相同的结构,那么你知道每个项目都有16行长,所以只需在第16行拆分它,你就会有2个有效的JSON对象。类似于这样:

import json

data = "" # your json

data_split = data.splitlines()
json_result = []
for data in range(0, len(data_split), 16):
    b = "".join(data_split[data:data+16])
    json_result.append(json.loads(b))

print(json_result)

字符串

4nkexdtk

4nkexdtk3#

下面是一个函数:

response = '''
{
"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"
}
'''

import json

def parse_response(response):
    response = response.strip()
    json_strs = response.split('\n{\n"self":')
    json_strs = [json_strs[0]] + ['{\n"self":' + s for s in json_strs[1:]]
    json_array_str = '[' + ','.join(json_strs) + ']'
    return json.loads(json_array_str)

parsed_data = parse_response(response)

print(parsed_data)

字符串

相关问题