在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()⽅法python json字符串转数组
以上这篇在python中将字符串转为json对象并取值的⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论