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中的socket模块使用代理实例

说socket代理之前,先来说说http代理,python的urllib2是自带http代理功能的,可以用如下代码实现:复制代码 代码如下:proxy_handler = urllib2...

Python中文件的读取和写入操作

从文件中读取数据 读取整个文件 这里假设在当前目录下有一个文件名为'pi_digits.txt'的文本文件,里面的数据如下: 3.1415926535 8979323846 2643...

浅谈flask源码之请求过程

Flask Flask是什么? Flask是一个使用 Python 编写的轻量级 Web 应用框架, 让我们可以使用Python语言快速搭建Web服务, Flask也被称为 "m...

python 快速排序代码

复制代码 代码如下: def quick_sort(ls): return [] if ls == [] else quick_sort([y for y in ls[1:] if y...

python创建只读属性对象的方法(ReadOnlyObject)

复制代码 代码如下:def ReadOnlyObject(**args):    dictBI = {}    args_n...