python 中json和字符串互相轉換


string =" {
  "status": "error",
  "messages": ["Could not find resource or operation 'BZK1.MapServer' on the system."],
  "code": 404

}"

print  '對象:' string

print '取值:' json.loads(string)['code']

輸出結果為:

對象:{u'status': u'error', u'code': 404, u'messages': [u"Could not find resource or operation 'BZK1.MapServer' on the system."]}

取值:404

將對象轉成字符串:

 

resultJson = {"state": 1}
print json.dumps(resultJson)
分別使用了Json包中的loads()方法和dumps()方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM