嵌套的JSON未在dict中加载为Keys

时间:2015-06-25 23:46:57

标签: python json

我目前在一个名为returned_data的dict中有以下JSON-我想获取帐户ID,但看起来它没有识别任何比第二级更深的密钥。例如:

returned_data["command1"]["customers"]

作品

returned_data["command1"]["customers"]["acctid"]

不起作用。

returned_data["command1"]["customers"].keys() 

不显示任何键。

我需要做些什么才能引用acctid?

{
"command1": {

        "customers": [{
            "city": "none",
            "cust_id": 204567,
            "name_first": "John",
            "name_last": "Smith",
            "zip": "39199",
            "street_addr_1": "1 Bat St",

            "phones": [
                {"phone_number": "(01) 5555555",
                "phone_type": "Mobile",
                "phone_code": "C"
                },
                {"phone_number": "(01) 5555555",
                "phone_type": "Home",
                "phone_code": "E"
                }
                    ],

            "email_addr": "test@test.com",
            "acctid": 123456,
            "state": "WA",
            "add_user": "JR",
            "country": "AUS",
            "acct_type": "P",
                }
                ],
        "ref": "123456",
        "result": 0
            },

"header": {"src_sys_type": 2,
    "ver": 1,
    "result": 0}

 }

1 个答案:

答案 0 :(得分:0)

这就是你需要的。

['command1']['customers'][0]['acctid']

您可能会收到索引错误。由于[0]之后的括号,因此需要'customers'。正如评论所说,它是一个列表,而不是一个json。