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

相关文章

Python3处理文件中每个词的方法

本文实例讲述了Python3处理文件中每个词的方法。分享给大家供大家参考。具体实现方法如下: ''''' Created on Dec 21, 2012 处理文件中的每个词 @...

使用XML库的方式,实现RPC通信的方法(推荐)

使用XML库的方式,实现RPC通信的方法(推荐)

1、先说结论:使用xml-rpc的机制可以很方便的实现服务器间的RPC调用。 2、试验结果如下: 3、源码如下: 服务器端的源代码如下: import operator, math...

pycharm 将python文件打包为exe格式的方法

pycharm 将python文件打包为exe格式的方法

因为近期正在学习python,就需要将python文件打包为exe可执行文件,就将该过程记录下来。 首先我是通过Pyinstall打包的,具体安装及打包步骤如下 1.打开终端控制台 通过...

Python元组拆包和具名元组解析实例详解

前言 在Python中元组是一个相较于其他语言比较特别的一个内置序列类型。有些python入门教程把元组成为“不可变的列表”,这种说法是不完备的,其并没有完整的概括元组的特点。除了用作不...

python中subprocess批量执行linux命令

可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 popen --废弃 commands --废弃,3....