使用Python访问JSON中的多个对象时遇到问题

时间:2015-05-08 19:35:32

标签: python json

我正在尝试解析Web服务并检索某些记录,但是我一直收到具有多个对象的响应的KeyError。这些记录是按时间间隔返回的,所以有时我可能会收到一条记录而其他记录可能会收到300条记录。如果我收到一条记录,我的代码逻辑就会起作用,如果有多个项目则代码不起作用。

以下是一个带有一个对象的输出示例。

{
    "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": {}
                }
            ]
        }
    }
}

代码如下;

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

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

如果我将上面的逻辑用于返回多个对象的响应,则会收到KeyError值,并且无法访问我要解析的数组。

返回多个对象的代码示例;

我使用时的输出;如果我尝试按照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''}]}

如何以与处理单个输出相同的方式处理多个输出?

3 个答案:

答案 0 :(得分:0)

你还没有提供一个完全运行的程序,但我猜你正在获取一个对象列表,你需要遍历列表,然后按照你对单个项目的方式索引每个对象。如果我将您粘贴的对象的两个副本放在一个列表中并将其存储在文件ì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

答案 1 :(得分:0)

我认为嵌套循环可以满足您的需求。 sr是外循环。 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

我认为问题是ListOfLa311ElectronicWaste是另一个列表 - 所以你需要遍历这个以访问它的元素

答案 2 :(得分:0)

我解决了这个问题,这个帖子是一个很大的参考 Decoding nested JSON with multiple 'for' loops

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

Y
N
N
N
N
N
Y
Y
N
Y
相关问题