我正在尝试解析嵌套的JSON数据,但是很难从大量嵌套的数据中获取文本
resp = platform.get('/restapi/v1.0/account/~/call-log', params)
print ((resp.text()))
cursor = mydb.cursor()
json_obj = json.loads((resp.text()))
for result in json_obj["records"]:
cursor.execute("INSERT INTO calldata (sessionID, startTime, fromName) VALUES (%s, %s, %s)",
(result["sessionId"],
result["startTime"],
result["from"]["name"]))
JSON输出
{
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/call-log?view=Simple&showBlocked=true&withRecording=false&dateFrom=2019-10-09T16:00:00.000Z&page=1&perPage=2",
"records" : [ {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/call-log/123456?view=Simple",
"id" : "123456",
"sessionId" : "123456",
"startTime" : "2019-10-09T20:47:26.577Z",
"duration" : 45,
"type" : "Voice",
"direction" : "Outbound",
"action" : "VoIP Call",
"result" : "Call connected",
"to" : {
"phoneNumber" : "123456"
},
"from" : {
"name" : "Jane Doe",
"phoneNumber" : "123456",
"extensionId" : "123456"
},
"recording" : {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/recording/123456",
"id" : "123456",
"type" : "Automatic",
"contentUri" : "https://media.ringcentral.com/restapi/v1.0/account/123456/recording/581514130067/content"
},
"extension" : {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/extension/1409182064",
"id" : 123456
}
}, {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/call-log/123456?view=Simple",
"id" : "123456",
"sessionId" : "123456",
"startTime" : "2019-10-09T20:37:49.540Z",
"duration" : 7,
"type" : "Voice",
"direction" : "Inbound",
"action" : "Phone Call",
"result" : "Missed",
"to" : {
"phoneNumber" : "123456"
},
"from" : {
"name" : "Bob Smith",
"phoneNumber" : "123456"
}
} ],
"paging" : {
"page" : 1,
"perPage" : 2,
"pageStart" : 0,
"pageEnd" : 1
},
"navigation" : {
"nextPage" : {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/call-log?view=Simple&showBlocked=true&withRecording=false&dateFrom=2019-10-09T16:00:00.000Z&page=2&perPage=2"
},
"firstPage" : {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/call-log?view=Simple&showBlocked=true&withRecording=false&dateFrom=2019-10-09T16:00:00.000Z&page=1&perPage=2"
},
"lastPage" : {
"uri" : "https://platform.ringcentral.com/restapi/v1.0/account/123456/call-log?view=Simple&showBlocked=true&withRecording=false&dateFrom=2019-10-09T16:00:00.000Z&page=1&perPage=2"
}
}
}
我得到的错误是
(result["sessionId"], result["startTime"], result["result"], result["direction"], result["duration"], result["from"]["name"])) KeyError: 'name'
我试图从“from”字典和“records”中获取数据。你知道吗
看看你的json,我会说问题是没有
result["name"]
键,但是有result["from"]["name"]
,试着用这个来代替。你知道吗如您所见,JSON的结构如下:
因此,如果你想访问
result["from"]["name"]
,你实际上并没有得到它。首先是records
->;然后是from
->;然后是name
。顺便说一下,记录是一个数组,所以您不必执行results['records']['from']['name']
如果要解析JSON,可以这样做:
输出:
使用^{} :
json_obj
中的所有内容records
不会展开records
创建包含顶层uri
的数据帧pd.merge
uri_x
是记录中的uri,并重命名为records.uri
uri_y
&;topuri
都是顶层uri
,它们在合并时被保留topuri
并将uri_y
重命名为top.uri
df.to_csv
或其他一些output options。你知道吗注:
records
(from
在记录中),那么您只需要以下数据帧df_rec = json_normalize(data, 'records')
,而不需要合并。你知道吗相关问题 更多 >
编程相关推荐