我有一个脚本,我希望可以使用NumPy在地理数据库中创建一个要素类。有几件事情正在发生,当我发送我的请求时,发送给服务器的关联json数据与我的响应不同,因此我无法确定我的关键值(如果有)。我最终想要一个键值并循环嵌套项目以用作要素类中的字段。JSON键值在Python中返回错误
代码:
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'
样品成功的请求输出全光照的PG只请求模块和JSON数据提交POST请求:
{"status":{"code":311,"message":"Service Request Successfully Submited","cause":""},"Response":{"PrimaryRowId":"1-3J1UX","ListOfServiceRequest":{"ServiceRequest":[{"SRNumber":"1-5927721"}]}}}
出于测试目的,我希望至少到输出SRNumber写信给我的要素类。
parsed_json是什么样的? – 2015-02-11 07:48:16
您不在此处发布*任何内容:'requests.post(url)'。您没有传入数据或标题。 – 2015-02-11 07:52:00
你的'parsed_json'变量包含响应json。你能在你的问题中发布第一个请求的回复吗? – thiruvenkadam 2015-02-11 08:01:09