首页 > 技术文章 > Python 笔记 v1 - json和字符串互转

ylz8401 2019-03-28 20:41 原文

字符串转为json对象并取值

>>> 使用了Json包中的loads()方法和dumps()方法
string =" {
  "status": "error",
  "messages": ["Could not find resource or operation 'BZK1.MapServer' on the system."],
  "code": 404

}"
print string

 

  •  String 转 json对象
json.loads(string)['code']

输出:

404

 

  • json对象转string

 

resultJson = {"state": 1}
print json.dumps(resultJson)

 

输出:

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

 

 


 

推荐阅读