简体   繁体   English

使用Python在JSON中访问数据多个对象时遇到问题

[英]Trouble accessing data multiple objects in JSON with Python

I am trying to parse through a web-service and retrieve certain records, however I am consistently receiving a KeyError for responses with more than one object. 我正在尝试通过Web服务解析并检索某些记录,但是我始终收到KeyError,以获取具有多个对象的响应。 These records are returned in intervals, so sometimes I might receive one record and others I might receive 300. If I receive one record, the logic of my code works, if there are multiple items then the code doesn't work. 这些记录是按间隔返回的,因此有时我可能会收到一条记录,而其他人可能会收到300条记录。如果我收到一条记录,则代码的逻辑有效,如果有多个项目,则代码不起作用。

Here is an example of an output with one object. 这是一个对象的输出示例。

{
    "status": {
        "code": 311,
        "message": "Service Request Successfully Queried.",
        "cause": ""
    },
    "Response": {
        "NumOutputObjects": "1",
        "ListOfServiceRequest": {
            "ServiceRequest": [
                {
                    "AddressVerified": "Y",
                    "SRNumber": "1-13967451",
                    "SRType": "Service Not Complete",
                    "CreatedDate": "05/08/2015 10:00:38",
                    "UpdatedDate": "05/08/2015 10:00:49",
                    "IntegrationId": "05082015100148678",
                    "Status": "Open",
                    "CreatedByUserLogin": "PROXYE",
                    "UpdatedByUserLogin": "PROXYE",
                    "Anonymous": "N",
                    "Zipcode": "90032",
                    "Latitude": "34.0843242531",
                    "Longitude": "-118.171015007",
                    "CustomerAccessNumber": "",
                    "LADWPAccountNo": "",
                    "NewContactFirstName": "jj",
                    "NewContactLastName": "rambo",
                    "NewContactPhone": "",
                    "NewContactEmail": "",
                    "ParentSRNumber": "1-10552271",
                    "Priority": "Normal",
                    "Language": "",
                    "ReasonCode": "",
                    "ServiceDate": "",
                    "Source": "",
                    "ClosedDate": "",
                    "Email": "",
                    "FirstName": "",
                    "HomePhone": "",
                    "LastName": "",
                    "LoginUser": "",
                    "ResolutionCode": "",
                    "SRUnitNumber": "",
                    "MobilOS": "",
                    "SRAddress": "5163 E TEMPLETON ST, 90032",
                    "SRAddressName": "",
                    "SRAreaPlanningCommission": "East Los Angeles APC",
                    "SRCommunityPoliceStation": "CENTRAL BUREAU",
                    "SRCouncilDistrictMember": "Jose Huizar",
                    "SRCouncilDistrictNo": "14",
                    "SRDirection": "E",
                    "SRNeighborhoodCouncilId": "48",
                    "SRNeighborhoodCouncilName": "LA-32 NC",
                    "SRStreetName": "TEMPLETON",
                    "SRSuffix": "ST",
                    "SRTBColumn": "F",
                    "SRTBMapGridPage": "595",
                    "SRTBRow": "6",
                    "SRXCoordinate": "6509897",
                    "SRYCoordinate": "1853117",
                    "AssignTo": "NC",
                    "Assignee": "NC eWaste Supervisor 01",
                    "Owner": "BOS",
                    "ParentSRStatus": "Open",
                    "ParentSRType": "Electronic Waste",
                    "ParentSRLinkDate": "05/08/2015 10:00:39",
                    "ParentSRLinkUser": "PROXYE",
                    "SRAreaPlanningCommissionId": "5",
                    "SRCommunityPoliceStationAPREC": "HOLLENBECK",
                    "SRCommunityPoliceStationPREC": "4",
                    "SRCrossStreet": "",
                    "ActionTaken": "",
                    "SRCity": "",
                    "RescheduleCounter": "",
                    "SRHouseNumber": "5163",
                    "SourceofRequestCouncil": "",
                    "CCBPremiseType": "",
                    "ContainerBlackCount": "",
                    "ContainerBrownCount": "",
                    "SRIntersectionDirection": "",
                    "SRApproximateAddress": "N",
                    "ContainerGreenCount": "",
                    "OtherBureauName": "",
                    "AssigneeName": "",
                    "AssigneeOrganization": "E-Waste, NC",
                    "AnotherBureauEmailId": "",
                    "ListOfLa311BarricadeRemoval": {},
                    "ListOfLa311BulkyItem": {},
                    "ListOfLa311DeadAnimalRemoval": {},
                    "ListOfLa311GraffitiRemoval": {},
                    "ListOfLa311InformationOnly": {},
                    "ListOfLa311MultipleStreetlightIssue": {},
                    "ListOfLa311SingleStreetlightIssue": {},
                    "ListOfLa311SrPhotoId": {
                        "La311SrPhotoId": []
                    },
                    "ListOfLa311BusPadLanding": {},
                    "ListOfLa311CurbRepair": {},
                    "ListOfLa311Flooding": {},
                    "ListOfLa311GeneralStreetInspection": {},
                    "ListOfLa311GuardWarningRailMaintenance": {},
                    "ListOfLa311GutterRepair": {},
                    "ListOfLa311LandMudSlide": {},
                    "ListOfLa311Pothole": {},
                    "ListOfLa311Resurfacing": {},
                    "ListOfLa311SidewalkRepair": {},
                    "ListOfLa311StreetSweeping": {},
                    "ListOfLa311BeesOrBeehive": {},
                    "ListOfLa311MedianIslandMaintenance": {},
                    "ListOfLa311OvergrownVegetationPlants": {},
                    "ListOfLa311PalmFrondsDown": {},
                    "ListOfLa311StreetTreeInspection": {},
                    "ListOfLa311StreetTreeViolations": {},
                    "ListOfLa311TreeEmergency": {},
                    "ListOfLa311TreeObstruction": {},
                    "ListOfLa311TreePermits": {},
                    "ListOfLa311BrushItemsPickup": {},
                    "ListOfLa311Containers": {},
                    "ListOfLa311ElectronicWaste": {},
                    "ListOfLa311IllegalDumpingPickup": {},
                    "ListOfLa311ManualPickup": {},
                    "ListOfLa311MetalHouseholdAppliancesPickup": {},
                    "ListOfLa311MoveInMoveOut": {},
                    "ListOfLa311HomelessEncampment": {},
                    "ListOfLa311IllegalAutoRepair": {},
                    "ListOfLa311IllegalConstruction": {},
                    "ListOfLa311IllegalConstructionFence": {},
                    "ListOfLa311IllegalDischargeOfWater": {},
                    "ListOfLa311IllegalDumpingInProgress": {},
                    "ListOfLa311IllegalExcavation": {},
                    "ListOfLa311IllegalSignRemoval": {},
                    "ListOfLa311IllegalVending": {},
                    "ListOfLa311LeafBlowerViolation": {},
                    "ListOfLa311NewsRackViolation": {},
                    "ListOfLa311Obstructions": {},
                    "ListOfLa311TablesAndChairsObstructing": {},
                    "ListOfLa311GisLayer": {
                        "La311GisLayer": [
                            {
                                "A_Call_No": "",
                                "Area": "0",
                                "Day": "MONDAY",
                                "DirectionSuffix": "",
                                "DistrictAbbr": "",
                                "DistrictName": "NC",
                                "DistrictNumber": "",
                                "DistrictOffice": "",
                                "Fraction": "",
                                "R_Call_No": "",
                                "SectionId": "",
                                "ShortDay": "",
                                "StreetFrom": "",
                                "StreetTo": "",
                                "StreetLightId": "",
                                "StreetLightStatus": "",
                                "Type": "GIS",
                                "Y_Call_No": "",
                                "Name": "05082015100148678100",
                                "CommunityPlanningArea": "",
                                "LastUpdatedBy": "",
                                "BOSRadioHolderName": ""
                            }
                        ]
                    },
                    "ListOfLa311ServiceRequestNotes": {
                        "La311ServiceRequestNotes": [
                            {
                                "CreatedDate": "05/08/2015 10:00:39",
                                "Comment": "Materials have been out in a normal collection area, unsure why driver missed the e-waste items.",
                                "CreatedByUser": "PROXYE",
                                "IsSrNoAvailable": "",
                                "CommentType": "External",
                                "Notification": "N",
                                "FeedbackSRType": "",
                                "IntegrationId": "050820151001486782",
                                "Date1": "",
                                "Date2": "",
                                "Date3": "",
                                "Text1": "",
                                "AnotherBureau": "",
                                "EmailAddress": "",
                                "ListOfLa311SrNotesAuditTrail": {}
                            },
                            {
                                "CreatedDate": "05/08/2015 10:00:39",
                                "Comment": "",
                                "CreatedByUser": "PROXYE",
                                "IsSrNoAvailable": "",
                                "CommentType": "Address Comments",
                                "Notification": "N",
                                "FeedbackSRType": "",
                                "IntegrationId": "050820151001486781",
                                "Date1": "",
                                "Date2": "",
                                "Date3": "",
                                "Text1": "",
                                "AnotherBureau": "",
                                "EmailAddress": "",
                                "ListOfLa311SrNotesAuditTrail": {}
                            }
                        ]
                    },
                    "ListOfLa311SubscribeDuplicateSr": {},
                    "ListOfChildServiceRequest": {},
                    "ListOfLa311BillingCsscAdjustment": {},
                    "ListOfLa311BillingEccAdjustment": {},
                    "ListOfLa311BillingRsscAdjustment": {},
                    "ListOfLa311BillingRsscExemption": {},
                    "ListOfLa311SanitationBillingBif": {},
                    "ListOfLa311SanitationBillingCssc": {},
                    "ListOfLa311SanitationBillingEcc": {},
                    "ListOfLa311SanitationBillingLifeline": {},
                    "ListOfLa311SanitationBillingRssc": {},
                    "ListOfLa311SanitationBillingSrf": {},
                    "ListOfLa311DocumentLog": {},
                    "ListOfAuditTrailItem2": {
                        "AuditTrailItem2": [
                            {
                                "Date": "05/08/2015 10:00:49",
                                "EmployeeLogin": "SADMIN",
                                "Field": "Assignee",
                                "NewValue": "NC eWaste Supervisor 01",
                                "OldValue": ""
                            }
                        ]
                    },
                    "ListOfLa311GenericBc": {
                        "La311GenericBc": [
                            {
                                "ATTRIB_08": "",
                                "NAME": "05082015100148678100",
                                "PAR_ROW_ID": "1-8BDCR",
                                "ROW_ID": "1-8BOCG",
                                "TYPE": "GIS",
                                "ATTRIB_16": "",
                                "ListOfLa311GenericbcAuditTrail": {}
                            },
                            {
                                "ATTRIB_08": "",
                                "NAME": "05082015100148678",
                                "PAR_ROW_ID": "1-8BDCR",
                                "ROW_ID": "1-8BOCJ",
                                "TYPE": "Service Not Complete",
                                "ATTRIB_16": "",
                                "ListOfLa311GenericbcAuditTrail": {}
                            }
                        ]
                    },
                    "ListOfLa311ServiceNotComplete": {
                        "La311ServiceNotComplete": [
                            {
                                "ContainerLocation": "",
                                "ContainerType": "",
                                "DriverFirstName": "",
                                "DriverLastName": "",
                                "MissedCollectionService": "Electronic Waste",
                                "OtherServiceMissedReason": "",
                                "ServiceDateRendered": "",
                                "ServiceMissedReason": "I'm not sure",
                                "TruckNo": "",
                                "Type": "Service Not Complete",
                                "WireBasketLocation": "",
                                "LastUpdatedBy": "",
                                "Name": "05082015100148678"
                            }
                        ]
                    },
                    "ListOfLa311Other": {},
                    "ListOfLa311WeedAbatementForPrivateParcels": {},
                    "ListOfLa311SanitationBillingInquiry": {}
                }
            ]
        }
    }
}

The code is below; 代码如下;

data2 = jsonpickle.decode((f2.read()))
Start = datetime.datetime.now()
data2 = jsonpickle.encode(data2)
url2 = "myURL"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}

r2 = requests.post(url2, data=data2, headers=headers2)
decoded2 = json.loads(r2.text)
try:
    r2
except requests.exceptions.ConnectTimeout as e:
    print "Too slow Mojo!"


items = []
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
    SRAddress = sr['SRAddress']
    Latitude = sr['Latitude']
    Longitude = sr['Longitude']
    ReasonCode = sr['ReasonCode']
    SRNumber = sr['SRNumber']
    FirstName = sr['FirstName']
    LastName = sr['LastName']
    ResolutionCode = sr['ResolutionCode']
    HomePhone = sr['HomePhone']
    CreatedDate = sr['CreatedDate']
    UpdatedDate = sr['UpdatedDate']
    CreatedDate = datetime.datetime.strptime(CreatedDate, "%m/%d/%Y %H:%M:%S")
    UpdatedDate = datetime.datetime.strptime(UpdatedDate, "%m/%d/%Y %H:%M:%S")
    print SRAddress
    print SRNumber


ItemInfo = " "
for ew in sr["ListOfLa311ServiceRequestNotes"][u"La311ServiceRequestNotes"]:
Comment = ew['Comment']
print Comment

Output Materials have been out in a normal collection area, unsure why driver missed the e-waste items. 输出Materials have been out in a normal collection area, unsure why driver missed the e-waste items.

If I use the logic above for a response with more than one object returned I receive a KeyError value and am unable to access the array that I want to parse. 如果我使用上面的逻辑对一个返回了多个对象的响应进行响应,则会收到KeyError值,并且无法访问要解析的数组。

Example of code with multiple objects returned; 返回多个对象的代码示例;

Output when I use; 使用时输出; I receive a key error if I attempt to do something along the lines of CommodityType = sr['ListOfLa311ElectronicWaste']['ElectronicWasteType'] below 如果我尝试按照以下CommodityType = sr['ListOfLa311ElectronicWaste']['ElectronicWasteType']方式执行操作,则会收到一个关键错误

for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
        CommodityType = sr['ListOfLa311ElectronicWaste']
        # ItemType = sr['ElectronicWestType']
        # DriverFirstName = sr ['DriverFirstName']
        # DriverLastName = sr ['DriverLastName']
        # ItemCount = sr['ItemCount']
        # ItemInfo += '{0},  {1}, '.format(ItemType, ItemCount)
        # ParentNumber = sr['Name']
        #
        print CommodityType




{u'La311ElectronicWaste': [{u'IllegallyDumped': u'N', u'OtherElectronicWestType': u'hash', u'ItemCount': u'5', u'Name': u'6a31f058-ece1-4e7d-b682-7d9052a512f4', u'MobileHomeSpace': u'', u'DriverLastName': u'', u'ActiveStatus': u'Y', u'DriverFirstName': u'', u'LastUpdatedBy': u'', u'GatedCommunityMultifamilyDwelling': u'Outside the main gate', u'IllegalDumpCollectionLoc': u'', u'ElectronicWestType': u'Other', u'CollectionLocation': u'Gated Community', u'Type': u'Electronic Waste', u'ServiceDateRendered': u'', u'TruckNo': u''}]}
{u'La311ElectronicWaste': [{u'IllegallyDumped': u'Y', u'OtherElectronicWestType': u'', u'ItemCount': u'5', u'Name': u'3f4d9d20-a712-4be3-822f-e6a45219c1cf', u'MobileHomeSpace': u'', u'DriverLastName': u'', u'ActiveStatus': u'Y', u'DriverFirstName': u'', u'LastUpdatedBy': u'', u'GatedCommunityMultifamilyDwelling': u'', u'IllegalDumpCollectionLoc': u'Cul De Sac', u'ElectronicWestType': u'Electronic Equipment', u'CollectionLocation': u'Alley', u'Type': u'Electronic Waste', u'ServiceDateRendered': u'', u'TruckNo': u''}]}

How do I handle multiple outputs the same way that I have handled the single output? 如何处理多个输出,就像处理单个输出一样?

You haven't provided a program that runs at all, but my guess is you're getting a list of objects, and you need to iterate through the list, then index each object the way you do with a single item. 您还没有提供一个完全可以运行的程序,但是我想您正在获取对象列表,并且需要遍历该列表,然后以对单个对象的方式对每个对象进行索引。 If I put two copies of the object you pasted in a list and store it in a file ìnput.json , I need to read each object like the following: 如果我将粘贴的对象的两个副本放在列表中,并将其存储在文件ìnput.json ,则需要读取每个对象,如下所示:

import json
with open("input.json") as f:
  data = json.load(f)
  for obj in data:
    for service_request in obj["Response"]['ListOfServiceRequest']['ServiceRequest']:
      print service_request # obviously do something useful instead

I think a nested loop will do what you require. 我认为嵌套循环将满足您的要求。 sr is the outer loop. sr是外循环。 ew should be an inner loop nested within sr all you do is ident the second loop. ew应该是嵌套在sr中的内部循环,您所做的就是确定第二个循环。

data2 = jsonpickle.decode((f2.read()))
Start = datetime.datetime.now()
data2 = jsonpickle.encode(data2)
url2 = "myURL"
headers2 = {'Content-type': 'text/plain', 'Accept': '/'}

r2 = requests.post(url2, data=data2, headers=headers2)
decoded2 = json.loads(r2.text)
try:
    r2
except requests.exceptions.ConnectTimeout as e:
    print "Too slow Mojo!"


items = []
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
    SRAddress = sr['SRAddress']
    Latitude = sr['Latitude']
    Longitude = sr['Longitude']
    ReasonCode = sr['ReasonCode']
    SRNumber = sr['SRNumber']
    FirstName = sr['FirstName']
    LastName = sr['LastName']
    ResolutionCode = sr['ResolutionCode']
    HomePhone = sr['HomePhone']
    CreatedDate = sr['CreatedDate']
    UpdatedDate = sr['UpdatedDate']
    CreatedDate = datetime.datetime.strptime(CreatedDate, "%m/%d/%Y %H:%M:%S")
    UpdatedDate = datetime.datetime.strptime(UpdatedDate, "%m/%d/%Y %H:%M:%S")
    print SRAddress
    print SRNumber


    ItemInfo = " "
    for ew in sr["ListOfLa311ServiceRequestNotes"][u"La311ServiceRequestNotes"]:
        Comment = ew['Comment']
        print Comment


    for ew in sr["ListOfLa311ElectronicWaste"][u"La311ElectronicWaste"]:
        IllegallyDumped = ew['IllegallyDumped']
        print IllegallyDumped

I think issue is that ListOfLa311ElectronicWaste is another list - so you need to iterate through this also to access its elements 我认为问题在于ListOfLa311ElectronicWaste是另一个列表-因此您还需要遍历该列表以访问其元素

I solved this, this thread was a huge reference. 我解决了这个问题,这个线程是一个巨大的参考。
Decoding nested JSON with multiple 'for' loops 解码带有多个“ for”循环的嵌套JSON

for sr in ElectronicType: for illegaldump in ElectronicType['La311ElectronicWaste']: illegalewaste = illegaldump['IllegallyDumped']

Y
N
N
N
N
N
Y
Y
N
Y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM