我试图遍历json-web服务,将每个对象的属性写入数据库。下面是我的代码片段和输出json。我想将每个对象的值写入数据库,即每个项目的值。现在我的代码片段只打印最后一个对象的值,所以" 1"对于ItemCount。
"ListOfLa311ElectronicWaste": {
"La311ElectronicWaste": [
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "",
"DriverLastName": "",
"ElectronicWestType": "Microwaves",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegallyDumped": "N",
"ItemCount": "3",
"MobileHomeSpace": "",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Electronic Waste",
"IllegalDumpCollectionLoc": "",
"LastUpdatedBy": "",
"Name": "021720151654176711"
},
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "",
"DriverLastName": "",
"ElectronicWestType": "Televisions (Any Size)",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegallyDumped": "N",
"ItemCount": "6",
"MobileHomeSpace": "",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Electronic Waste",
"IllegalDumpCollectionLoc": "",
"LastUpdatedBy": "",
"Name": "021720151654176722"
},
{
"CollectionLocation": "Gated Community/Multifamily Dw",
"DriverFirstName": "",
"DriverLastName": "",
"ElectronicWestType": "VCR/DVD Players",
"GatedCommunityMultifamilyDwelling": "Curb",
"IllegallyDumped": "N",
"ItemCount": "1",
"MobileHomeSpace": "",
"OtherElectronicWestType": "",
"ServiceDateRendered": "",
"TruckNo": "",
"Type": "Electronic Waste",
"IllegalDumpCollectionLoc": "",
"LastUpdatedBy": "",
"Name": "021720151654176723"
}
]
},
代码:
f2 = open('C:\Users\Administrator\Desktop\DetailView.json', 'r')
data2 = jsonpickle.encode( jsonpickle.decode(f2.read()) )
url2 = "https://myla311.lacity.org/myla311router/mylasrbe/1/QuerySR"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}
r2 = requests.post(url2, data=data2, headers=headers2)
decoded2 = json.loads(r2.text)
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
SRAddress = sr['SRAddress']
latitude = sr['Latitude']
longitude = sr['Longitude']
for ew in sr["ListOfLa311ElectronicWaste"][u"La311ElectronicWaste"]:
CommodityType = ew['Type']
ItemType = ew['ElectronicWestType']
ItemCount = ew['ItemCount']
答案 0 :(得分:0)
我试图发现为什么ItemCount正在为LAEwaste311中的最后一个JSON对象打印,其数量为1.
我的初始打印声明为print ItemCount
我能够用`print ew ['ItemCount']来解决这个问题,它按照上面的JSON中的说明返回了每个项目的3,6,1。