Python 笔记 v1 - json和字符串互转

时间:2023-03-10 01:57:52
Python 笔记 v1 - json和字符串互转

字符串转为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."]}