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."]}

 

 


 

posted @ 2019-03-28 20:41  ck_2016  Views(186)  Comments(0Edit  收藏  举报