在python中将字符串转为json对象并取值的方法

yipeiwu_com6年前Python基础

如下所示:

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设计】。

相关文章

Python中字典与恒等运算符的用法分析

本文实例讲述了Python中字典与恒等运算符的用法。分享给大家供大家参考,具体如下: 字典 字典是可变数据类型,其中存储的是唯一键到值的映射。 elements = {"hydrog...

python计算对角线有理函数插值的方法

本文实例讲述了python计算对角线有理函数插值的方法。分享给大家供大家参考。具体实现方法如下: ''' p = rational(xData,yData,x) Evaluate...

python字符串中的单双引

python中字符串可以(且仅可以)使用成对的单引号、双引号、三个双引号(文档字符串)包围: 'this is a book'  "this is a book" """thi...

tensorflow实现加载mnist数据集

tensorflow实现加载mnist数据集

mnist作为最基础的图片数据集,在以后的cnn,rnn任务中都会用到 import numpy as np import tensorflow as tf import matpl...

Python可变参数*args和**kwargs用法实例小结

Python可变参数*args和**kwargs用法实例小结

本文实例讲述了Python可变参数*args和**kwargs用法。分享给大家供大家参考,具体如下: 一句话简单概括:当函数的参数不确定的时候就需要用到*args和**kwargs,前者...