使用python解析json文件(转)
2017-11-03 18:53
836 查看
转自:http://blog.chinaunix.net/uid-9525959-id-3074355.html
更多的信息,可以参考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]
说明一下,我在按上边的这个方法解析从客户端发过来的json数据时一直出问题,总是解析不出来,后边才发现,json格式的字符串是不能用'(单引号)来表示字符串的,字符串必须用"(双引号),所以在我的代码中使用了str.replace('\'','\"')后就可以正常解析了。
更多的信息,可以参考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]
说明一下,我在按上边的这个方法解析从客户端发过来的json数据时一直出问题,总是解析不出来,后边才发现,json格式的字符串是不能用'(单引号)来表示字符串的,字符串必须用"(双引号),所以在我的代码中使用了str.replace('\'','\"')后就可以正常解析了。
相关文章推荐
- 使用python解析json文件
- 使用python解析json文件
- 使用Python解析JSON文件
- 使用python解析json文件
- 使用python解析json文件
- 使用python解析json文件 分类: python 小练习 2014-03-21 18:17 354人阅读 评论(0) 收藏
- 使用python解析.frp文件
- Python之使用ElementTree解析xml文件
- 使用Python 解析XML文件
- 使用"JSONKit.h"解析文件
- 【原】使用Json作为Python和C#混合编程时对象转换的中间文件
- Python_使用csv模块解析csv文件(处理Excel表格)
- 使用python解析火狐的书签备份文件
- Python中使用ConfigParser解析ini配置文件实例
- python解析json字符串和json文件的区别
- 使用.NET4.0的dynamic特性解析plist文件及json字符串
- 使用C++语言来解析json文件
- 在c++中使用json----保存到文件、从文件中解析
- Python 获取接口数据,解析JSON,写入文件
- python解析json文件读取Android permission说明