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

yipeiwu_com5年前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编码类型转换方法。分享给大家供大家参考,具体如下: 1:Python和unicode 为了正确处理多语言文本,Python在2.0版后引入了Unicode字符串...

Appium+Python自动化测试之运行App程序示例

Appium+Python自动化测试之运行App程序示例

在上一篇博客中,已经将环境搭建好了。现在,我们利用搭建的环境来运行一条测试脚本,脚本中启动一个计算器的应用,并实现加法的运算。 创建模拟器 在运行App之前,首先需要创建一个Androi...

Python3用tkinter和PIL实现看图工具

Python3用tkinter和PIL实现看图工具

需求 想做看图工具的,必然要支持jpg、png等常见格式,但tkinter是个纯粹的GUI库,不像GTK、QT那样大而全,所以只支持gif和ppm两种格式,局限很大,必须搭配图像处理库,...

python3图片转换二进制存入mysql

首先,在数据库中创建一个表,用于存放图片:复制代码 代码如下:CREATE TABLE Images(Id INT PRIMARY KEY AUTO_INCREMENT, Data ME...

python实现合并两个数组的方法

本文实例讲述了python实现合并两个数组的方法。分享给大家供大家参考。具体如下: python合并两个数组,将两个数组连接成一个数组,例如,数组 a=[1,2,3] ,数组 b=[4,...