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中处理字符串之islower()方法的使用简介

 islower()方法判断检查字符串的所有的字符(字母)是否为小写。 语法 以下是islower()方法的语法: str.islower() 参数  ...

Python实现的径向基(RBF)神经网络示例

本文实例讲述了Python实现的径向基(RBF)神经网络。分享给大家供大家参考,具体如下: from numpy import array, append, vstack, tran...

利用python和ffmpeg 批量将其他图片转换为.yuv格式的方法

由于跑编码的需要,所以需要制作一个.yuv格式的图片数据集,但是手头只有.jpg格式的,故记录下转换过程。其他图片格式也可以,代码里修改一下就行。 ①安装ffmpeg 官网(各种版本):...

查看python安装路径及pip安装的包列表及路径

查看python安装路径及pip安装的包列表及路径

一、Linux系统 查看Python路径 whereis python 此命令将会列出系统所安装的所有版本的Python的路径效果如下:  使用以下命令可分别查看Pytho...

用Python实现BP神经网络(附代码)

用Python实现BP神经网络(附代码)

用Python实现出来的机器学习算法都是什么样子呢? 前两期线性回归及逻辑回归项目已发布(见文末链接),今天来讲讲BP神经网络。 BP神经网络 全部代码 https://github.c...