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

相关文章

python读取一个目录下所有txt里面的内容方法

实例如下所示: import os allFileNum = 0 def printPath(level, path): global allFileNum ''''' 打印一...

python编写计算器功能

python编写计算器功能

本文实现用python编写一个带界面的计算器小程序,当然这个计算器功能很简单,只能进行一些简单的数学运算,很适合初学者,希望能给大家带来一些启发 实验前提 因为是带图形界面的,所以...

python中单下划线(_)和双下划线(__)的特殊用法

函数使用单下划线_开头   使用单下划线(_)开头的函数_func不能被模块外部以: from module import *形式导入。   但可以用:from module imp...

Python中用于转换字母为小写的lower()方法使用简介

 lower()方法返回所有基于大小写的字符被转换为小写字符串的一个副本。 语法 以下是lower()方法的语法: str.lower() 参数  &...

Python中的is和==比较两个对象的两种方法

Python中的is和==比较两个对象的两种方法 在Python中有两种方式比较两个对象是否相等,分别是is和==,两者之间是不同的 ==比较的是值(如同java中的equals方...