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 导入上级目录中的模块实例

python导入同级别模块很方便: import xxx 要导入下级目录页挺方便,需要在下级目录中写一个__init__.py文件 from dirname import xx...

跟老齐学Python之复习if语句

基本语句结构 复制代码 代码如下: if 判断条件1:     执行语句1…… elif 判断条件2:     执行语句2……...

Python中二维列表如何获取子区域元素的组成

Python中二维列表如何获取子区域元素的组成

用过NumPY的应该都知道,在二维数组中可以方便地使用区域切片功能,如下图: 而这个功能在Python标准库的List中是不支持的,在List中只能以一维方式来进行切片操作: 但有时...

详解Python3.6的py文件打包生成exe

详解Python3.6的py文件打包生成exe

原文提到的要点: 1. Python版本32位 (文件名为 python-3.6.1.exe) 2. 安装所有用到的模块(原文博主用的是openpyxl,我用到的有urllib中的req...

替换python字典中的key值方法

比如有一个 a = {‘a': 1} 希望变为 a = {‘b' :1} 即:在保留value不变的情况下,替换key值 目前能想到的实现方案是 a[‘b'] = a.p...