在Python中处理POST请求的多个JSON数据集

时间:2015-02-12 00:20:23

标签: python json

我有3个JSON方法,它们是两个查询方法和一个更新方法。我想解析这些信息并执行POST请求,并使用arcpy将这些数据传递到数据库中,以便GIS用于所有三种方法。我有一个工作并获得响应的脚本,但问题是找到每个对象的键和值,以便我可以成功将我的值发送到数据库。

此外,我使用三种不同的方法处理此任务,所有这些方法都需要来自。

的数据

例如, 查询1将允许我解析并找到地址,lat / lng等。 查询2将允许我解析并查找客户信息,请求类型等。 查询3将允许我更新请求。

我的第一个问题是如何成功地从每个输出中仅提取我想要的数据;我在POSTman中测试了键/值无济于事,服务器期待整个JSON文件。

我的第二个问题是如何处理3个不同的请求;我假设3种不同的后期方法并选择我想要的数据。

传递给服务器的JSON one查询示例

{
    "RequestSpecificDetail": {
        "ParentSRNumberForLink": ""
    },
    "MetaData": {
        "appVersion": "1.34",
        "deviceModel": "x86_64",
        "dateAndTime": "01/15/2015 12:46:36",
        "deviceToken": "A2C1DD9D-D17D-4031-BA3E-977C250BFD58",
        "osVersion": "8.1"
    },
    "SRData": {
        "LoginUser": "User89",
        "NewContactEmail": "abc@gmail.com",
        "UpdatedDate": "02/05/2015"
    }
}

查询1输出的示例

{
    "status": {
        "code": 311,
        "message": "Service Request Successfully Queried.",
        "cause": ""
    },
    "Response": {
        "NumOutputObjects": "2",
        "ListOfServiceRequest": {
            "ServiceRequest": [
                {
                    "SRAddress": "1200 W TEMPLE ST, 90026",
                    "SRNumber": "1-5099871",
                    "SRType": "Feedback",
                    "CreatedDate": "02/05/2015 22:55:58",
                    "UpdatedDate": "02/05/2015 22:55:58",
                    "Status": "Open",
                    "imageURL": ""
                },
                {
                    "SRAddress": "1200 W TEMPLE ST, 90026",
                    "SRNumber": "1-5133051",
                    "SRType": "Feedback",
                    "CreatedDate": "02/05/2015 23:03:54",
                    "UpdatedDate": "02/05/2015 23:03:54",
                    "Status": "Open",
                    "imageURL": "https://SERVER_END_POINT/portal/docview?id=fe083ae14b52b1af0945b4d756c296a5"
                }
            ]
        },
        "LastUpdateDate": "02/05/2015"
    }
}

传递给服务器的查询2的示例

{
    "RequestSpecificDetail": {
        "ParentSRNumberForLink": ""
    },
    "MetaData": {
        "appVersion": "1.34",
        "deviceModel": "x86_64",
        "dateAndTime": "01/15/2015 12:46:36",
        "deviceToken": "A2C1DD9D-D17D-4031-BA3E-977C250BFD58",
        "osVersion": "8.1"
    },
    "SRData": {
        "SRNumber": "1-1080871"
    }
}

查询两个输出

{
    "status": {
        "code": 311,
        "message": "Service Request Successfully Queried.",
        "cause": ""
    },
    "Response": {
        "NumOutputObjects": "1",
        "ListOfServiceRequest": {
            "ServiceRequest": [
                {
                    "AddressVerified": "Y",
                    "SRNumber": "1-1080871",
                    "SRType": "Homeless Encampment",
                    "CreatedDate": "12/31/2014 13:49:23",
                    "UpdatedDate": "12/31/2014 13:49:23",
                    "IntegrationId": "1420033765921",
                    "Status": "Open",
                    "CreatedByUserLogin": "User89",
                    "UpdatedByUserLogin": "User89",
                    "Anonymous": "N",
                    "Zipcode": "90026",
                    "Latitude": "34.064937",
                    "Longitude": "-118.252968",
                    "CustomerAccessNumber": "",
                    "LADWPAccountNo": "",
                    "NewContactFirstName": "",
                    "NewContactLastName": "",
                    "NewContactPhone": "",
                    "NewContactEmail": "",
                    "ParentSRNumber": "",
                    "Priority": "Normal",
                    "Language": "English",
                    "ReasonCode": "",
                    "ServiceDate": "12/31/2014 00:00:00",
                    "Source": "311",
                    "Email": "user@email.com",
                    "FirstName": "User",
                    "HomePhone": "3123123123",
                    "LastName": "Pp",
                    "LoginUser": "",
                    "ResolutionCode": "",
                    "SRUnitNumber": "",
                    "MobilOS": "iOS",
                    "SRAddress": "1200 W TEMPLE ST, 90026",
                    "SRAddressName": "",
                    "SRAreaPlanningCommission": "Central APC",
                    "SRCommunityPoliceStation": "",
                    "SRCouncilDistrictMember": "Gilbert Cedillo",
                    "SRCouncilDistrictNo": "1",
                    "SRDirection": "W",
                    "SRNeighborhoodCouncilId": "44",
                    "SRNeighborhoodCouncilName": "GREATER ECHO PARK ELYSIAN NC",
                    "SRStreetName": "TEMPLE",
                    "SRSuffix": "ST",
                    "SRTBColumn": "E",
                    "SRTBMapGridPage": "634",
                    "SRTBRow": "2",
                    "SRXCoordinate": "6485064",
                    "SRYCoordinate": "1846114",
                    "AssignTo": "North Central - 104 - IED",
                    "Assignee": "Siebel Administrator",
                    "Owner": "BSS",
                    "ParentSRStatus": "",
                    "ParentSRType": "",
                    "ParentSRLinkDate": "",
                    "ParentSRLinkUser": "",
                    "SRAreaPlanningCommissionId": "4",
                    "SRCommunityPoliceStationAPREC": "RAMPART",
                    "SRCommunityPoliceStationPREC": "2",
                    "SRCrossStreet": "",
                    "ActionTaken": "",
                    "SRCity": "",
                    "RescheduleCounter": "",
                    "SRHouseNumber": "",
                    "ListOfDataBarricadeRemoval": {},
                    "ListOfDataBulkyItem": {},
                    "ListOfDataDeadAnimalRemoval": {},
                    "ListOfDataGraffitiRemoval": {},
                    "ListOfDataInformationOnly": {},
                    "ListOfDataMultipleStreetlightIssue": {},
                    "ListOfDataSingleStreetlightIssue": {},
                    "ListOfDataSrPhotoId": {
                        "DataSrPhotoId": []
                    },
                    "ListOfDataBusPadLanding": {},
                    "ListOfDataCurbRepair": {},
                    "ListOfDataFlooding": {},
                    "ListOfDataGeneralStreetInspection": {},
                    "ListOfDataGuardWarningRailMaintenance": {},
                    "ListOfDataGutterRepair": {},
                    "ListOfDataLandMudSlide": {},
                    "ListOfDataPothole": {},
                    "ListOfDataResurfacing": {},
                    "ListOfDataSidewalkRepair": {},
                    "ListOfDataStreetSweeping": {},
                    "ListOfDataBeesOrBeehive": {},
                    "ListOfDataMedianIslandMaintenance": {},
                    "ListOfDataOvergrownVegetationPlants": {},
                    "ListOfDataPalmFrondsDown": {},
                    "ListOfDataStreetTreeInspection": {},
                    "ListOfDataStreetTreeViolations": {},
                    "ListOfDataTreeEmergency": {},
                    "ListOfDataTreeObstruction": {},
                    "ListOfDataTreePermits": {},
                    "ListOfDataBrushItemsPickup": {},
                    "ListOfDataContainers": {},
                    "ListOfDataElectronicWaste": {},
                    "ListOfDataIllegalDumpingPickup": {},
                    "ListOfDataManualPickup": {},
                    "ListOfDataMetalHouseholdAppliancesPickup": {},
                    "ListOfDataMoveInMoveOut": {},
                    "ListOfDataHomelessEncampment": {
                        "DataHomelessEncampment": [
                            {
                                "ApprovedBy": "",
                                "AssignedTo": "",
                                "CompletedBy": "",
                                "Contact": "",
                                "ContactDate": "",
                                "Crew": "",
                                "DateCompleted": "12/31/2014 00:00:00",
                                "InspectedBy": "",
                                "InspectionDate": "",
                                "Location": "Alley",
                                "Type": "Homeless Encampment",
                                "LastUpdatedBy": "",
                                "OptionalTrackingCode": "",
                                "Name": "a5b5b2b9-d2e7-400a-bf75-1138ff013caa"
                            }
                        ]
                    },
                    "ListOfDataIllegalAutoRepair": {},
                    "ListOfDataIllegalConstruction": {},
                    "ListOfDataIllegalConstructionFence": {},
                    "ListOfDataIllegalDischargeOfWater": {},
                    "ListOfDataIllegalDumpingInProgress": {},
                    "ListOfDataIllegalExcavation": {},
                    "ListOfDataIllegalSignRemoval": {},
                    "ListOfDataIllegalVending": {},
                    "ListOfDataLeafBlowerViolation": {},
                    "ListOfDataNewsRackViolation": {},
                    "ListOfDataObstructions": {},
                    "ListOfDataTablesAndChairsObstructing": {},
                    "ListOfDataGisLayer": {
                        "DataGisLayer": [
                            {
                                "A_Call_No": "",
                                "Area": "",
                                "Day": "",
                                "DirectionSuffix": "",
                                "DistrictAbbr": "",
                                "DistrictName": "Central",
                                "DistrictNumber": "104",
                                "DistrictOffice": "North Central",
                                "Fraction": "",
                                "R_Call_No": "",
                                "SectionId": "5279800",
                                "ShortDay": "",
                                "StreetFrom": "BOYLSTON ST",
                                "StreetTo": "FIRMIN ST",
                                "StreetLightId": "",
                                "StreetLightStatus": "",
                                "Type": "GIS",
                                "Y_Call_No": "",
                                "Name": "41572025-3803-49c4-8561-6e7ef41775df",
                                "CommunityPlanningArea": "Westlake",
                                "LastUpdatedBy": "",
                                "BOSRadioHolderName": ""
                            }
                        ]
                    },
                    "ListOfDataServiceRequestNotes": {
                        "DataServiceRequestNotes": [
                            {
                                "CreatedDate": "12/31/2014 13:49:23",
                                "Comment": "",
                                "CreatedByUser": "User89",
                                "IsSrNoAvailable": "N",
                                "CommentType": "External",
                                "Notification": "N",
                                "FeedbackSRType": "",
                                "IntegrationId": "1420033765921",
                                "Date1": "",
                                "Date2": "",
                                "Date3": "",
                                "Text1": "",
                                "ListOfDataSrNotesAuditTrail": {}
                            }
                        ]
                    },
                    "ListOfDataSubscribeDuplicateSr": {
                        "DataSubscribeDuplicateSr": [
                            {
                                "Activeflag": "Y",
                                "EmailId": "pratik.desai@yoopmail.com",
                                "Name": "010420150405",
                                "Type": "Subscription",
                                "LastUpdatedBy": ""
                            }
                        ]
                    },
                    "ListOfChildServiceRequest": {},
                    "ListOfDataBillingCsscAdjustment": {},
                    "ListOfDataBillingEccAdjustment": {},
                    "ListOfDataBillingRsscAdjustment": {},
                    "ListOfDataBillingRsscExemption": {},
                    "ListOfDataSanitationBillingBif": {},
                    "ListOfDataSanitationBillingCssc": {},
                    "ListOfDataSanitationBillingEcc": {},
                    "ListOfDataSanitationBillingInquiry": {},
                    "ListOfDataSanitationBillingLifeline": {},
                    "ListOfDataSanitationBillingRssc": {},
                    "ListOfDataSanitationBillingSrf": {},
                    "ListOfDataDocumentLog": {},
                    "ListOfAuditTrailItem2": {},
                    "ListOfDataGenericBc": {
                        "DataGenericBc": [
                            {
                                "ATTRIB_08": "",
                                "NAME": "41572025-3803-49c4-8561-6e7ef41775df",
                                "PAR_ROW_ID": "1-N607",
                                "ROW_ID": "1-N60A",
                                "TYPE": "GIS",
                                "ListOfDataGenericbcAuditTrail": {}
                            },
                            {
                                "ATTRIB_08": "",
                                "NAME": "a5b5b2b9-d2e7-400a-bf75-1138ff013caa",
                                "PAR_ROW_ID": "1-N607",
                                "ROW_ID": "1-N609",
                                "TYPE": "Homeless Encampment",
                                "ListOfDataGenericbcAuditTrail": {}
                            },
                            {
                                "ATTRIB_08": "",
                                "NAME": "010420150405",
                                "PAR_ROW_ID": "1-N607",
                                "ROW_ID": "1-RN2D",
                                "TYPE": "Subscription",
                                "ListOfDataGenericbcAuditTrail": {}
                            }
                        ]
                    },
                    "ListOfDataServiceNotComplete": {},
                    "ListOfDataOther": {},
                    "ListOfDataWeedAbatementForPrivateParcels": {}
                }
            ]
        }
    }
}

查询3输入

{
    "MetaData": {},
    "RequestSpecificDetail": {
        "ParentSRNumberForLink": ""
    },
    "SRData": {
        "SRNumber":"1-5968841",
        "Anonymous": "N",
        "Assignee": "",
        "CreatedByUserLogin": "KAHUNA30DEC",
        "CustomerAccessNumber": "",
        "LADWPAccountNo": "",
        "Language": "English",
        "ListOfDataGisLayer": {},
        "ListOfDataServiceRequestNotes": {
            "DataServiceRequestNotes": [
                {
                    "Comment": "description 1245",
                    "CommentType": "Feedback",
                    "CreatedByUser": "KAHUNA30DEC",
                    "FeedbackSRType": "Illegal Dumping in Progress",
                    "IsSrNoAvailable": "N"
                },
                {
                    "Comment": "comments 123568",
                    "CommentType": "External",
                    "CreatedByUser": "",
                    "IsSrNoAvailable": "N"
                }
            ]
        },
        "LoginUser": "KAHUNA30DEC",
        "MobilOS": "Android",
        "NewContactEmail": "",
        "NewContactFirstName": "",
        "NewContactLastName": "",
        "NewContactPhone": "",
        "Owner": "Other",
        "ParentSRNumber": "",
        "Priority": "Normal",
        "SRCommunityPoliceStation": "RAMPART",
        "UpdatedByUserLogin": "KAHUNA30DEC",
        "Status": "Open",
        "SRType": "Feedback",
        "ServiceDate": "02/11/2015",
        "Source": "Mobile App"
    }
}

查询3输出

{
    "status": {
        "code": 311,
        "message": "Service Request Successfully Submited",
        "cause": ""
    },
    "Response": {
        "PrimaryRowId": "1-3JXL5",
        "ListOfServiceRequest": {
            "ServiceRequest": [
                {
                    "SRNumber": "1-5968841"
                }
            ]
        }
    }
}

Python脚本使用查询二输出

进行响应
import json
import jsonpickle
import arcpy
import json
import numpy
import requests





f = open('C:\Users\Administrator\Desktop\myData.json', 'r')

data = jsonpickle.encode( jsonpickle.decode(f.read()) )

url = "https://myDatatest.lacity.org/myDatarouter/srbe/1/QuerySR"
headers = {'Content-type': 'text/plain', 'Accept': '/'}

r = requests.post(url, data=data, headers=headers)
sr = arcpy.SpatialReference(4326)


decoded = json.loads(r.text)


# pretty printing of json-formatted string
print json.dumps(decoded, sort_keys=True, indent=4)





  f.close()

1 个答案:

答案 0 :(得分:0)

decoded是一个包含您正在寻找的数据的字典。 尝试:

print decoded
print decoded.keys()
print decoded.items()