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实现合并字典的方法。分享给大家供大家参考。具体实现方法如下: # 将两个字典合并 #!/usr/bin/python def adddict(dict1,d...

浅谈五大Python Web框架

浅谈五大Python Web框架

说到Web Framework,Ruby的世界Rails一统江湖,而Python则是一个百花齐放的世界,各种micro-framework、framework不可胜数,不完全列表见: h...

nginx黑名单和django限速,最简单的防恶意请求方法分享

nginx黑名单和django限速,最简单的防恶意请求方法分享

django项目遭遇cc攻击,不要惊慌,这里推荐两招简单实用的技巧。 项目Nginx作为http接入层,分发到django应用,启动10个uwsgi worker。 今日突然发现,网页打...

python 读文件,然后转化为矩阵的实例

代码流程: 1. 从文件中读入数据。 2. 将数据转化成矩阵的形式。 3. 对于矩阵进行处理。 具体的python代码如下: - 文件路径需要设置正确。 - 字符串处理。 - 字符串数...

详解Python 2.6 升级至 Python 2.7 的实践心得

前言 CentOS 6.8 安装 Python 2.7.13,因为软件版本上的需求所以考虑将 Python 升级至 2.7.13,加上生产环境还是以 RHEL 6 为主,互联网自动化运...