我有一個腳本,我希望可以使用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