Python中返回错误的JSON键值
我有一个脚本,我希望与NumPy一起使用它在地理数据库中创建要素类。当我发送请求时,我发送到服务器的相关json数据与我的响应不同,因此我无法确定我的键值是什么(如果有的话)。我最终希望有一个键值并循环遍历嵌套项,以用作要素类中的字段 代码: JSON发送到服务器:Python中返回错误的JSON键值,python,json,numpy,Python,Json,Numpy,我有一个脚本,我希望与NumPy一起使用它在地理数据库中创建要素类。当我发送请求时,我发送到服务器的相关json数据与我的响应不同,因此我无法确定我的键值是什么(如果有的话)。我最终希望有一个键值并循环遍历嵌套项,以用作要素类中的字段 代码: JSON发送到服务器: { "MetaData": {}, "RequestSpecificDetail": { "ParentSRNumberForLink": "" },
{
"MetaData": {},
"RequestSpecificDetail": {
"ParentSRNumberForLink": ""
},
"SRData": {
"Anonymous": "Y",
"Assignee": "",
"CreatedByUserLogin": "",
"CustomerAccessNumber": "",
"LADWPAccountNo": "",
"Language": "English",
"ListOfLa311GisLayer": {},
"ListOfLa311ServiceRequestNotes": {
"La311ServiceRequestNotes": [
{
"Comment": "hxhdudi",
"CommentType": "Feedback",
"FeedbackSRType": "Weed Abatement for Pvt Parcels",
"IsSrNoAvailable": "N"
},
{
"Comment": "",
"CommentType": "External",
"CreatedByUser": "",
"IsSrNoAvailable": "N"
}
]
},
"LoginUser": "",
"MobilOS": "Android",
"NewContactEmail": "",
"NewContactFirstName": "",
"NewContactLastName": "",
"NewContactPhone": "",
"Owner": "Other",
"ParentSRNumber": "",
"Priority": "Normal",
"SRCommunityPoliceStation": "RAMPART",
"SRType": "Feedback",
"ServiceDate": "01/22/2015",
"Source": "Mobile App",
"Status": "Open",
"UpdatedByUserLogin": ""
}
}
line 37, in <module>
for item in parsed_json["La311ServiceRequestNotes"]:
KeyError: 'La311ServiceRequestNotes'
{"status":{"code":311,"message":"Service Request Successfully Submited","cause":""},"Response":{"PrimaryRowId":"1-3J1UX","ListOfServiceRequest":{"ServiceRequest":[{"SRNumber":"1-5927721"}]}}}
错误:
{
"MetaData": {},
"RequestSpecificDetail": {
"ParentSRNumberForLink": ""
},
"SRData": {
"Anonymous": "Y",
"Assignee": "",
"CreatedByUserLogin": "",
"CustomerAccessNumber": "",
"LADWPAccountNo": "",
"Language": "English",
"ListOfLa311GisLayer": {},
"ListOfLa311ServiceRequestNotes": {
"La311ServiceRequestNotes": [
{
"Comment": "hxhdudi",
"CommentType": "Feedback",
"FeedbackSRType": "Weed Abatement for Pvt Parcels",
"IsSrNoAvailable": "N"
},
{
"Comment": "",
"CommentType": "External",
"CreatedByUser": "",
"IsSrNoAvailable": "N"
}
]
},
"LoginUser": "",
"MobilOS": "Android",
"NewContactEmail": "",
"NewContactFirstName": "",
"NewContactLastName": "",
"NewContactPhone": "",
"Owner": "Other",
"ParentSRNumber": "",
"Priority": "Normal",
"SRCommunityPoliceStation": "RAMPART",
"SRType": "Feedback",
"ServiceDate": "01/22/2015",
"Source": "Mobile App",
"Status": "Open",
"UpdatedByUserLogin": ""
}
}
line 37, in <module>
for item in parsed_json["La311ServiceRequestNotes"]:
KeyError: 'La311ServiceRequestNotes'
{"status":{"code":311,"message":"Service Request Successfully Submited","cause":""},"Response":{"PrimaryRowId":"1-3J1UX","ListOfServiceRequest":{"ServiceRequest":[{"SRNumber":"1-5927721"}]}}}
出于测试目的,我至少希望将输出SRNumber写入要素类。您不需要先选择
SRData
字典吗
parsed_json["SRData"]["La311ServiceRequestNotes"]
究竟什么是
解析的_json
?因为它给了你一个键错误,所以它是一本字典。它的键是什么?解析的json看起来像什么?您没有在这里发布任何内容:请求。post(url)
。您没有传递数据或标题。您的parsed_json
变量包含响应json。你能在你的问题中发布第一个请求的响应吗?@thiruvenkadam:第一个请求可能不会产生任何合理的结果,因为发送的post
正文将是空的。我希望第一个请求可能会给出完整的元数据,而第二个请求更像是过滤掉的响应。如果没有,解析的json
应该是用于response.json()
而不是r.json()
,并且应该在以后的阶段给出。我误解了这一点,但是将我解析的json设置为parsed\u json=[“SRData”[“la311servicequestnotes”]
抛出parsed\u json=[“SRData”[“la311servicequestnotes”]
TypeError:列表索引必须是整数,而不是str'
什么是=['…']
应该做什么?这就是解析的json的问题,我一直无法找出键。