Python 笔记 v1

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



原文地址:https://www.cnblogs.com/ylz8401/p/10617517.html