2017-12-18 129 views
-2

我想用json.dumps自動輸出hostname字段的值。json.dumps的用法

{ 
    "code": 200, 
    "success": true, 
     "data": [{ 
     "xxx": "xxxx", 
     "xxxx": "xxx", 
     "xxxx": "xxxx", 
     "xxxx": "xxxx", 
     "xxxx": "xxxx", 
     "hostname": None, 
     "xxxx": "xxxx", 
     "xxxx": "xxxx", 
     "xxxx": "xxxx", 
     "xxxx": "xxxx", 
     "xxxx": [{ 
      "xxxx": "xxxx", 
      "xxxx": "xxxx" 
     }] 
     }] 
} 

預先感謝您

+1

歡迎堆棧溢出!請[參觀],環顧四周,並閱讀[幫助],特別是[*我如何提出一個好問題?](/幫助/如何問) –

回答

0

這是不清楚的問題,但我儘量給你一個答案。你可以試試這個:

import json 
import sys 

data={"code": 200, "success": True, "data": [{"xxx": "xxxx", "xxxx": "xxx", "xxxx": "xxxx", "xxxx": "xxxx", "xxxx": "xxxx", "hostname": None, "xxxx": "xxxx", "xxxx": "xxxx", "xxxx": "xxxx", "xxxx": "xxxx", "xxxx": [{"xxxx": "xxxx", "xxxx": "xxxx"}]}]} 

json_str = json.dumps(data) 
resp = json.loads(json_str) 

print (resp) 
print (resp['data'][0]['hostname']) 

比的結果將是:

{u'code': 200, u'data': [{u'xxx': u'xxxx', u'hostname': None, u'xxxx': [{u'xxxx': u'xxxx'}]}], u'success': True} 
None 
相關問題