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

yipeiwu_com5年前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中的单引号双引号有什么区别呢?下面给大家详细的介绍一下: 先说双引号与三引号的区别,双引号所表示的字符串通常要写成一行。 如: s1 = "hello,world" 如...

Python一键查找iOS项目中未使用的图片、音频、视频资源

Python一键查找iOS项目中未使用的图片、音频、视频资源

前言 在iOS项目开发的过程中,如果版本迭代开发的时间比较长,那么在很多版本开发以后或者说有多人开发参与以后,工程中难免有一些垃圾资源,未被使用却占据着api包的大小! 这里我通过Pyt...

Python3正则匹配re.split,re.finditer及re.findall函数用法详解

本文实例讲述了Python3正则匹配re.split,re.finditer及re.findall函数用法。分享给大家供大家参考,具体如下: re.split re.finditer r...

python运行时间的几种方法

最早见过手写的,类似于下面这种: import datetime def time_1(): begin = datetime.datetime.now() sum =...

Python自动连接ssh的方法

本文实例讲述了Python自动连接ssh的方法。分享给大家供大家参考。具体实现方法如下: #!/usr/bin/python #-*- coding:utf-8 -*- import...