我正在尝试解析嵌套的JSON数据。我正在尝试获取每个键的“ DisplayValue”,我的代码是
json_obj = r.json()
for result in json_obj["Result"]:
for employeeid in result["EmployeeId"]:
cursor.execute("INSERT INTO employee_detail (EmployeeId) VALUES (%s)",
(result["DisplayValue"]))
但是我得到答复
KeyError'DisplayValue'
JSON输出
{
"IsError": "false",
"Status": 0,
"Message": "string",
"Result": [
{
"EmployeeId": {
"DisplayValue": "PW180",
"FieldHistory": []
},
"Title": {
"DisplayValue": "Mr.",
"FieldHistory": []
},
谢谢:)
答案 0 :(得分:1)
您有一个嵌套的字典。
使用:
json_obj = r.json()
for result in json_obj["Result"]:
cursor.execute("INSERT INTO employee_detail (EmployeeId) VALUES (%s)",
(result['EmployeeId']["DisplayValue"],))