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中使用PDB库调试程序

Python自带的pdb库,发现用pdb来调试程序还是很方便的,当然了,什么远程调试,多线程之类,pdb是搞不定的。 用pdb调试有多种方式可选: 1. 命令行启动目标程序,加上-m参数...

在python 中split()使用多符号分割的例子

调用re模块中的split()函数可以用多个符号进行分割 In [1]: import re In [2]: words = '我,来。上海?吃?上海菜' In [3]: wor...

通过Python 接口使用OpenCV的方法

一、在 Anaconda2 中配置 OpenCV 解压 opencv,添加系统环境变量,计算机-->右键属性-->高级系统设置-->环境变量-->系统变量--&g...

pytest中文文档之编写断言

编写断言 使用assert编写断言 pytest允许你使用python标准的assert表达式写断言; 例如,你可以这样做: # test_sample.py def func...

python脚本内运行linux命令的方法

本文实例讲述了python脚本内运行linux命令的方法。分享给大家供大家参考。具体实现方法如下: #/usr/bin/env python import subprocess cl...