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解决这个任务,请帮助。
3条答案
按热度按时间a0zr77ik1#
您的数据包含多个JSON对象。可以通过检测仅包含}的行和仅包含{的相邻行来分隔它们
因此:
字符串
kwvwclae2#
奇怪的是,像API这样的东西用看起来像JSON但不是有效JSON的东西来响应。
无论如何,如果JSON结构遵循相同的结构,那么你知道每个项目都有16行长,所以只需在第16行拆分它,你就会有2个有效的JSON对象。类似于这样:
字符串
4nkexdtk3#
下面是一个函数:
字符串