2015-06-25 20 views
0

我有一個下面的JSON當前在一個字典中叫做returned_data-我想獲取帳戶ID,但它看起來好像沒有識別出比第二級更深的任何鍵。例如:嵌套的JSON不加載爲字典中的Keys

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": "[email protected]", 
      "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} 

} 
+2

'returned_data [「command1」] [「customers」]'是一個列表,而不是一個字典。這是足夠的,還是你需要知道如何在Python中使用列表? –

+0

看起來我需要工作我對列表的知識 - 謝謝! – tororm

回答

0

這就是你需要的。

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

您可能會收到索引錯誤。由於'customers'之後的括號,需要[0]。正如評論所說,這是一個列表,而不是一個json。

+0

完美 - 謝謝! – tororm