我有一个脚本,我希望与NumPy一起使用,在地理数据库中创建一个要素类。有几件事情正在发生,当我发送请求时,我发送给服务器的相关json数据与我的响应不同,因此我无法确定我的键值是什么,如果有的话。我最终希望有一个键值并循环遍历嵌套项目,以用作要素类中的字段。
代码:
import requests
import json
import jsonpickle
import arcpy
import json
import numpy
import requests
fc = "C:\MYLATesting.gdb\MYLA311"
if arcpy.Exists(fc):
arcpy.Delete_management(fc)
f = open('C:\Users\Administrator\Desktop\myla311.json', 'r')
data = jsonpickle.encode( jsonpickle.decode(f.read()) )
url = "myURL.com"
headers = {'Content-type': 'text/plain', 'Accept': '/'}
r = requests.post(url)
parsed_json = r.json()
sr = arcpy.SpatialReference(4326)
response = requests.post(url, data=data, headers=headers)
f.close()
ndtype = numpy.dtype([
('Comment', 'S48')
])
vehicles = []
for item in parsed_json["La311ServiceRequestNotes"]:
vehicles.append(tuple(item[k] for k in ndtype.names))
narr = numpy.array(vehicles, ndtype)
arcpy.da.NumPyArrayToFeatureClass(narr, fc, [34.1728677, -118.5389413], sr)
print response.text
发送到服务器的JSON:
{
"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'
仅使用请求模块和JSON作为提交POST请求的数据的成功请求输出示例:
{"status":{"code":311,"message":"Service Request Successfully Submited","cause":""},"Response":{"PrimaryRowId":"1-3J1UX","ListOfServiceRequest":{"ServiceRequest":[{"SRNumber":"1-5927721"}]}}}
出于测试目的,我至少希望将输出SRNumber写入我的要素类。
答案 0 :(得分:0)
首先,您不需要选择SRData
词典吗?
parsed_json["SRData"]["La311ServiceRequestNotes"]
究竟是什么parsed_json
?因为它给你一个KeyError,它是一个字典。它的关键是什么?