Python-JSON循环只打印最后一个obj的值

2024-10-02 22:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过jsonweb服务循环,将每个对象的属性写入数据库。下面是我的代码片段和输出json。我想将每个对象的值写入数据库,即每个项的值。现在我的代码片段只打印最后一个对象的值,因此ItemCount的值为“1”。你知道吗

"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']

Tags: 对象代码communityjsontypedwsrew
1条回答
网友
1楼 · 发布于 2024-10-02 22:26:45

我试图找出为什么ItemCount会为LAEwaste311中的最后一个JSON对象打印,这个对象的数量是1。你知道吗

我最初的打印语句是print ItemCount

我可以用print ew['ItemCount']来解决这个问题,它返回了上面JSON中提到的每个项目的3,6,1。你知道吗

相关问题 更多 >