Python json.loads()返回列表,而不是dict

时间:2019-07-14 20:12:39

标签: json python-3.x

我有一个来自我通过request.get()获取的API的以下json。 现在,我想使用json.loads()将这个json转换成字典,但结果是一个列表。 来自https://www.w3schools.com/python/python_json.asp的结果应该是命令...还是我在这里错了?谢谢!

    import requests
    import json

    r = requests.get('http://url/api,                  
    auth=requests.auth.HTTPBasicAuth('user','pass'))

    x = json.loads(r.text)

    # type(x) >>> <class 'list'>


json:
[
    {
        "id": 400285,
        "statusId": 1,
        "deliveryAddress": {
            "firstName": "Admin",
            "lastName": "Admin",
            "company": "Testshop 2",
            "street": "Teststr. 1",
            "houseNumber": "",
            "additionalAddressInfo": "",
            "postcode": "12345",
            "city": "Testort",
            "state": "Bremen",
            "country": "Germany",
            "countryIsoCode": "DE"
        },
        "billingAddress": {
            "firstName": "Admin",
            "lastName": "Admin", 
        }
    },
    {
        "id": 400288,
        "statusId": 1,
        "deliveryAddress": {
            "firstName": "Carolin",
            "lastName": "Pr\u00f6ger",
            "company": "",
            "street": "teststra\u00dfe 12",
            "houseNumber": "",
            "additionalAddressInfo": "",
            "postcode": "12345",
            "city": "Bremen",
            "state": "Bremen",
            "country": "Germany",
            "countryIsoCode": "DE"
        },
        "billingAddress": {
            "firstName": "Carolin",
            "lastName": "Pr\u00f6ger",
        }
    }
]

Edit1:

mydict= {}

for item in mylist:
    mydict['ID'] = item['id']
    mydict['statusID'] = item['statusId']

1 个答案:

答案 0 :(得分:-1)

这是因为您的json响应包含列表而不是对象。