2015-02-11 169 views
0

我有一個腳本,我希望可以使用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寫信給我的要素類。

+0

parsed_json是什麼樣的? – 2015-02-11 07:48:16

+0

您不在此處發佈*任何內容:'requests.post(url)'。您沒有傳入數據或標題。 – 2015-02-11 07:52:00

+0

你的'parsed_json'變量包含響應json。你能在你的問題中發佈第一個請求的回覆嗎? – thiruvenkadam 2015-02-11 08:01:09

回答

0

您是不是首先需要選擇SRData字典?

parsed_json["SRData"]["La311ServiceRequestNotes"] 

到底什麼是parsed_json?由於它給你一個KeyError,它是一個字典。它的鑰匙是什麼?

+0

這會引發類似的錯誤。 – 2015-02-11 17:14:24

+0

鑰匙是什麼? – hpaulj 2015-02-11 17:42:10

+0

我誤解了這個,但是設置我的解析的json爲'parsed_json = [「SRData」] [「La311ServiceRequestNotes」]'throws'parsed_json = [「SRData」] [「La311ServiceRequestNotes」]' TypeError:list indices必須是整數,不是str'' – 2015-02-11 17:47:41