尝试在for循环中解析JSON对象时,为什么会收到错误消息?

时间:2019-06-05 04:03:56

标签: python json python-requests

在我尝试通过for循环运行脚本之前,脚本的所有操作都可以正常运行。具体来说,当我尝试索引对象内的特定数组时。在开始之前,该脚本旨在获取列表中每个跟踪号的交货日期。

这是我的脚本:

import requests
import json

TrackList = ['1Z3X756E0310496105','1ZX0373R0303581450','1ZX0373R0103574417']
url = 'https://onlinetools.ups.com/rest/Track'
para1 = '...beginning of JSON request string...'
para2 = '...end of JSON request string...'

for TrackNum in TrackList:
    parameters = para1+TrackNum+para2
    resp = requests.post(url = url, data = parameters, verify=False)
    data = json.loads(resp.text)
    DelDate = data['TrackResponse']['Shipment']['Package'][0]['Activity'][0]['Date']
    print(DelDate)

JSON API响应(如果需要):

{
    "TrackResponse": {
        "Response": {
            "ResponseStatus": {
                "Code": "1",
                "Description": "Success"
            },
            "TransactionReference": {
                "CustomerContext": "Analytics Inquiry"
            }
        },
        "Shipment": {
            "InquiryNumber": {
                "Code": "01",
                "Description": "ShipmentIdentificationNumber",
                "Value": "1ZX0373R0103574417"
            },
            "Package": {
                "Activity": [
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "OKLAHOMA CITY",
                                "CountryCode": "US",
                                "PostalCode": "73128",
                                "StateProvinceCode": "OK"
                            },
                            "Code": "M3",
                            "Description": "Front Desk",
                            "SignedForByName": "CUMMINGS"
                        },
                        "Date": "20190520",
                        "Status": {
                            "Code": "9E",
                            "Description": "Delivered",
                            "Type": "D"
                        },
                        "Time": "091513"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "Oklahoma City",
                                "CountryCode": "US",
                                "StateProvinceCode": "OK"
                            },
                            "Description": "Front Desk"
                        },
                        "Date": "20190520",
                        "Status": {
                            "Code": "OT",
                            "Description": "Out For Delivery Today",
                            "Type": "I"
                        },
                        "Time": "085943"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "Oklahoma City",
                                "CountryCode": "US",
                                "StateProvinceCode": "OK"
                            },
                            "Description": "Front Desk"
                        },
                        "Date": "20190520",
                        "Status": {
                            "Code": "DS",
                            "Description": "Destination Scan",
                            "Type": "I"
                        },
                        "Time": "011819"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "Oklahoma City",
                                "CountryCode": "US",
                                "StateProvinceCode": "OK"
                            },
                            "Description": "Front Desk"
                        },
                        "Date": "20190519",
                        "Status": {
                            "Code": "AR",
                            "Description": "Arrival Scan",
                            "Type": "I"
                        },
                        "Time": "235100"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "DFW Airport",
                                "CountryCode": "US",
                                "StateProvinceCode": "TX"
                            },
                            "Description": "Front Desk"
                        },
                        "Date": "20190519",
                        "Status": {
                            "Code": "DP",
                            "Description": "Departure Scan",
                            "Type": "I"
                        },
                        "Time": "195500"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "City": "DFW Airport",
                                "CountryCode": "US",
                                "StateProvinceCode": "TX"
                            },
                            "Description": "Front Desk"
                        },
                        "Date": "20190517",
                        "Status": {
                            "Code": "OR",
                            "Description": "Origin Scan",
                            "Type": "I"
                        },
                        "Time": "192938"
                    },
                    {
                        "ActivityLocation": {
                            "Address": {
                                "CountryCode": "US"
                            },
                            "Description": "Front Desk"
                        },
                        "Date": "20190517",
                        "Status": {
                            "Code": "MP",
                            "Description": "Order Processed: Ready for UPS",
                            "Type": "M"
                        },
                        "Time": "184621"
                    }
                ],
                "PackageWeight": {
                    "UnitOfMeasurement": {
                        "Code": "LBS"
                    },
                    "Weight": "2.00"
                },
                "ReferenceNumber": [
                    {
                        "Code": "01",
                        "Value": "8472745558"
                    },
                    {
                        "Code": "01",
                        "Value": "5637807:1007379402:BN81-17077A:1"
                    },
                    {
                        "Code": "01",
                        "Value": "5637807"
                    }
                ],
                "TrackingNumber": "1ZX0373R0103574417"
            },
            "PickupDate": "20190517",
            "Service": {
                "Code": "001",
                "Description": "UPS Next Day Air"
            },
            "ShipmentAddress": [
                {
                    "Address": {
                        "AddressLine": "S 600 ROYAL LN",
                        "City": "COPPELL",
                        "CountryCode": "US",
                        "PostalCode": "750193827",
                        "StateProvinceCode": "TX"
                    },
                    "Type": {
                        "Code": "01",
                        "Description": "Shipper Address"
                    }
                },
                {
                    "Address": {
                        "City": "OKLAHOMA CITY",
                        "CountryCode": "US",
                        "PostalCode": "73128",
                        "StateProvinceCode": "OK"
                    },
                    "Type": {
                        "Code": "02",
                        "Description": "ShipTo Address"
                    }
                }
            ],
            "ShipmentWeight": {
                "UnitOfMeasurement": {
                    "Code": "LBS"
                },
                "Weight": "2.00"
            },
            "ShipperNumber": "X0373R"
        }
    }
}

以下是我收到的错误:

Traceback (most recent call last):
  File "/Users/***/Library/Preferences/PyCharmCE2019.1/scratches/UPS_API.py", line 15, in <module>
    DelDate = data['TrackResponse']['Shipment']['Package'][0]['Activity'][0]['Date']
KeyError: 0

2 个答案:

答案 0 :(得分:4)

您试图在索引0处索引“包”,但这是一个对象而不是数组。因此,您应该访问['Package']['Activity']

答案 1 :(得分:0)

因为没有[1]或[2],所以只需带走[0]