python解析json示例代码

发布时间:2020-01-13编辑:脚本学堂
本文分享一个python编程中解析json数据的例子,学习下python json数据解析的方法,有需要的朋友参考下。

更多信息,请参考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]