Python操作json数据的一个简单例子

yipeiwu_com6年前Python基础

更多的信息,可以参考python内部的json文档:
python>>> help(json)
或者官方文档: http://docs.python.org/library/json.html#module-json.

下面给出一个使用python解析json的简单例子:

复制代码 代码如下:

#!/usr/bin/python
import json
#Function:Analyze json script
#Json is a script can descript data structure as xml,
#for detail, please refer to "http://json.org/json-zh.html".

#Note:
#1.Also, if you write json script from python,
#you should use dump instead of load. pleaser refer to "help(json)".

#json file:
#The file content of temp.json is:
#{
# "name":"00_sample_case1",
# "description":"an example."
#}
#f = file("temp.json");
#s = json.load(f)
#print s
#f.close

#json string:
s = json.loads('{"name":"test", "type":{"name":"seq", "parameter":["1", "2"]}}')
print s
print s.keys()
print s["name"]
print s["type"]["name"]
print s["type"]["parameter"][1]

PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在线格式化工具:
http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.jb51.net/code/jsoncodeformat

在线json压缩/转义工具:

http://tools.jb51.net/code/json_yasuo_trans

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

相关文章

解决Python计算矩阵乘向量,矩阵乘实数的一些小错误

解决Python计算矩阵乘向量,矩阵乘实数的一些小错误

计算:Ax-b A: 2*2 x: 2*1 b: 2*1 so, Ax-b: 2*1 if __name__ == "__main__": A = np.array([[...

快速解决pyqt5窗体关闭后子线程不同时退出的问题

用pyqt5设计了一个主窗体,在窗体运行时需要把一个无限循环放在一个线程去工作。运行后,发现通过鼠标按主窗体的关闭按键关闭主创体后,线程不会自动终止,依然在运行。尽管对我的使用场景来说,...

python如何通过protobuf实现rpc

python如何通过protobuf实现rpc

由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc。rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的...

Python3.6日志Logging模块简单用法示例

Python3.6日志Logging模块简单用法示例

本文实例讲述了Python3.6日志Logging模块简单用法。分享给大家供大家参考,具体如下: Logging是一个很方便的模块,用来打印日志 我直接列出一个最灵活的方法 # -*...

python求素数示例分享

复制代码 代码如下:# 判断是否是素数def is_sushu(num): res=True for x in range(2,num-1):  ...