Python解析json学习


python json 中文

python中自带了处理python的模块,使用时候直接import json即可。使用loads方法即可将json字符串转换成python对象。

一、但在使用json模块的时候需要注意的是对中文的处理,loads方法如果传入的字符串的编码不是UTF-8的话,需要用encoding指定字符编码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
js = json.loads('{"insun": "泰囧 / 人在囧途2 / Lost in Thailand "}')
print json.dumps(js)
print json.dumps(js,ensure_ascii=False)

打印效果如下:
{“insun”: “u6cf0u56e7 / u4ebau5728u56e7u90142 / Lost in Thailand “}
{“insun”: “泰囧 / 人在囧途2 / Lost in Thailand “}

二、如果传入的字符串的编码不是UTF-8的话,需要用encoding指定字符编码。对于:
dataDict = json.loads(dataJsonStr);
其中dataJsonStr是json字符串,如果其编码本身是非UTF-8的话,比如是GB2312的,那么上述代码,就会导致出错。改为对应的:
dataDict = json.loads(dataJsonStr, encoding=”GB2312”);
就可以了。

此处,即对应着上面函数解释中的:

If s is a str instance and is encoded with an ASCII based encoding other than UTF-8 (e.g. latin-1), then an appropriate encoding name must be specified.

三、如果要解析的字符串,本身的编码类型,不是基于ASCII的,那么,调用json.loads之前,需要先将对应字符串,转换为Unicode类型。
还是以上述的:
dataDict = json.loads(dataJsonStr, encoding=”GB2312″);
为例,即使你此处的字符串dataJsonStr,已经通过encoding指定了合适的编码,但是由于其中,包含了其他的编码的字符,比如我本身 dataJsonStr 是GB2312的字符,但是其中又包含了的一些日文字符,此时,json.loads还是会出错,因为此处的 dataJsonStr 不是以ASCII为基础的字符编码,所以,需要先去将dataJsonStr转换为Unicode,然后再调用 json.loads,就可以了。

代码如下:

dataJsonStrUni = dataJsonStr.decode("GB2312");
dataDict = json.loads(dataJsonStrUni, encoding="GB2312");

 

官网:
18.2. json — JSON encoder and decoder
http://docs.python.org/2/library/json.html

json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True,cls=None, indent=None, separators=None, encoding=”utf-8″, default=None, sort_keys=False, **kw)
json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[,object_pairs_hook[, **kw]]]]]]]])

参考:

————————-

Python解析json数据结构范例
一、JSON的格式:

1,对象:
{name:”Peggy”,email:”[email protected]”,homepage:”http://www.peggy.com”}
{ 属性 : 值 , 属性 : 值 , 属性 : 值 }

2,数组是有顺序的值的集合。一个数组开始于”[“,结束于”]”,值之间用”,”分隔。
[
{name:”Peggy”,email:”[email protected]”,homepage:”http://www.peggy.com”}, {name:”Peggy”,email:”[email protected]”,homepage:”http://www.peggy.com”},
{name:”Peggy”,email:”[email protected]”,homepage:”http://www.peggy.com”}
]

3, 值可以是字符串、数字、true、false、null,也可以是对象或数组。这些结构都能嵌套。

4,json示例:

import json
# Converting Python to JSON
json_object = json.write( python_object )
# Converting JSON to Python
python_object = json.read( json_object )

5,simplejson 示例:

import simplejson
# Converting Python to JSON
json_object = simplejson.dumps( python_object )
# Converting JSON to Python
python_object = simplejson.loads( json_object )

二、python从web接口上查询信息

1,先看个例子
>>> import urllib
>>> url=’http://www.example.com/’
>>> page=urllib.urlopen(url)
>>> data=page.read()
>>> print data //这个就是json的数据结构,str类型
{“total”:1,”data”:[{“oume”:””,”asNum”:”33333″,”cabinet”:”22″,”dMel”:”rr”,”hostname”:”h1.bkeep.com”,”logicSite”:”99″,”menfo”:{“amount”:4,”size”:8192},”ip”:”2.16.20.13″,”isOnline”:true,”useState”:”888″,”serviceTag”:”222X”,”cpuInfo”:{“amount”:2,”masterFrequency”:1995,”model”:”Intel(R) Xeon(R) CPU E5405 @ 2.00GHz”,”coreNum”:8,”l2CacheSize”:6144},”cabinetPositionNum”:””,”buyTime”:”2009-06-29″,”manageIp”:”2.31.58.223″,”idc”:”腾讯机房”,”resperson”:”马化腾”}],”errorMsg”:””,”isSuccess”:true}

>>> type(data)
<type ‘str’>

2,有了json数据结构,我却不知道怎么把它解析出来,幸亏有了李建辉的指导。大概思路是:

首先,json基本上是key/value的,python中就叫字典。既然是字典,那就应该安照读字典的方式去读。
将上面的data转为字典类型,这里用json模块的read方法。

>>> import json
>>> ddata=json.read(data)
>>> ddata
{‘isSuccess’: True, ‘errorMsg’: ”, ‘total’: 1, ‘data’: [{‘isOnline’: True, ‘idc’: ‘xe6x9dxadxe5xb7f’, ‘assetsNum’: ‘227003’, ‘responsibilityPerson’: ‘xe4xb9’, ‘deviceModel’: ‘a 1950’, ‘serviceTag’: ‘233’, ‘ip’: ‘2.1.20.163’, ‘hostname’: ‘h.bkeep.com’, ‘manageIp’: ‘2.31.58.223’, ‘cabinet’: ‘H05’, ‘buyTime’: ‘2009-06-29’, ‘useState’: ‘xe4xbd’, ‘memoryInfo’: {‘amount’: 4, ‘size’: 8192}, ‘cpuInfo’: {‘coreNum’: 8, ‘l2CacheSize’: 6144, ‘amount’: 2, ‘model’: ‘Intel(R) Xeon(R) CPU E5405 @ 2.00GHz’, ‘mastncy’: 1995}, ‘cabineionNum’: ”, ‘outGuime’: ”, ‘logicSite’: ‘xe46x969’}]}
>>>

看看ddata已经是dict类型了
>>> type(ddata)
<type ‘dict’>

其次,我们以读字典中key 为”data”对应的键值
>>> ddata[‘data’] //查看字典的方法!
[{‘isOnline’: True, ‘idc’: ‘xe6x9dxadxee6x88xbf’, ‘assetsNum’: ‘B50070100007003’, ‘responsibilityPerson’: ‘xe5xafx9a’, ‘deviceModel’: ‘PowerEdge 1950’, ‘serviceTag’: ‘7292X’, ‘ip’: ‘2.16.20.163’, ‘hostname’: ‘h1.bkeep.com’, ‘manageIp’: ‘2.31.58.223’, ‘cabinet’: ‘H05’, ‘buyTime’: ‘2009-06-29’, ‘useState’: ‘xe4xbdxbfe4xb8xad’, ‘memoryInfo’: {‘amount’: 4, ‘size’: 8192}, ‘cpuInfo’: {‘coreNum’: 8, ‘l2Caze’: 6144, ‘amount’: 2, ‘model’: ‘Intel(R) Xeon(R) CPU E5405 @ 2.00GHz’, ‘masterFrequency’: 1995}, ‘cabinetonNum’: ”, ‘outeTime’: ”, ‘logicSite’: ’87xe7xabx99′}]

>>>type(ddata[‘data’])
<type ‘list’>

发现ddata[‘data’]是一个列表,列表就要用序号来查询
>>> ddata[‘data’][0] //查看列表的方法!
{‘isOnline’: True, ‘idc’: ‘xe6x9d’, ‘assetsNum’: ‘07003’, ‘resposon’: ‘xe5xbc\xafx9a’, ‘deviceModel’: ‘PowerEdge 1950’, ‘serviceTag’: ‘7002X’, ‘ip’: ‘2.16.20.163’, ‘hostname’: ‘h1.bkeep.com’, ‘manageIp’: ‘2.31.58.223’, ‘cabinet’: ‘H05’, ‘buyTime’: ‘2009-06-29’, ‘useState’: ‘x94xa8xe4x8xad’, ‘memoryInfo’: {‘amount’: 4, ‘size’: 8192}, ‘cpuInfo’: {‘coreNum’: 8, ‘l2CacheSize’: 6144, ‘amount’: 2, ‘model’: ‘Intel(R) Xeon(R) CPU E5405 @ 2.00GHz’, ‘masterFrequency’: 1995}, ‘cabinetPx99’}
>>>

呵呵,ddata[‘data’]列表的0号元素是个字典。。

好,那我们查查key为idc的键值是多少
>>> ddata[‘data’][0][‘idc’] //查看字典的方法!
‘xe6x8xbf’

>>> print ddata[‘data’][0][‘idc’]
深圳腾讯机房

看到这里终于明白怎么解析json数据结构了。。。那就是“一层一层往下剥

【Python】 如何解析json数据结构 – bkeep的日志 – 网易博客 http://bkeep.blog.163.com/blog/static/12341429020113156582685/
==============================
———————————————————-

#!/usr/bin/env python
#coding=utf-8
import json

s = '[{"name":"鸟巢","point":{"lat":"39.990","lng":"116.397"},"desc":"奥运会主场地"},{"name":"北大乒乓球馆","point":{"lat":"39.988","lng":"116.315"},"desc":"乒乓球比赛场地"},{"name":"北京工人体育场","point": {"lat":"39.930","lng":"116.446"},"desc":"足球比赛场地"}]'

locations = json.read(s)

#注:read是json-py.py和minijson.py的方法,而python2.6开始自带的lib库里用的是simplejson.py,其没有read方法,而是load/loads
print str(len(locations)) #因为json.load()之后的对象就是dict类型的!
for location in locations:
    print location["name"]
    print location["point"]["lat"]

———————————————————-

#!/usr/local/bin/python
#coding = utf-8
import os

import json Res='[{"brief":"ooooo","class_extid":13,"create_time":1131783174,"face_id":4,"flag":6777217,"flag_ext":0,"level":0,"max_member":100,"memo":"m~F~M0m~Zm~@~B","name":"10502","option":2,"ul":[{"flag":4,"u":285},{"flag":4,"u":35}]}]'
qDic = json.loads(Res)

for i in qDic:
    print i["class_extid"]
for j in i["ul"]:
    print str(j["flag"]) + "===" + str(j["u"])

==========================================================

更多范例请参考 http://docs.python.org/library/json.html

=======================================
>>> import json
>>> dir(json)
[‘JSONDecoder’, ‘JSONEncoder’, ‘__all__’, ‘__author__’, ‘__builtins__’, ‘__doc__’, ‘__file__’, ‘__name__’, ‘__package__’, ‘__path__’, ‘__version__’, ‘_default_decoder’, ‘_default_encoder’, ‘decoder’, ‘dump’, ‘dumps’, ‘encoder’, ‘load’, ‘loads’, ‘scanner’]

———————-

#!/usr/bin/env python
# coding=utf-8
import json

f = file("baidu_nav_json.txt"); #baidu_nav_json.txt是我的百度首页中的导航,查看源码,将其内容保存下来存为文件(utf-8格式)
s = json.load(f)
#print type(s)
print str(len(s)) # 4
print s.keys() # [u'errNo', u'data', u'isNplus', u'defaultDirId']

#print s["data"]
#print s["data"][10]["navs"][0]
#print s["data"][10]["navs"][0]["name"], s["data"][10]["navs"][0]["url"]

for i in range(0, len(s["data"])):
    print s["data"][i]["dirName"]
    for j in range(0, len(s["data"][i]["navs"])):
        print s["data"][i]["navs"][j]["name"], s["data"][i]["navs"][j]["url"]
    print
####
f.close

———————-
下面给出一个使用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]

 


《“Python解析json学习”》 有 3 条评论

  1. Python2.6 开始加入了JSON模块,无需另外下载,Python的Json模块序列化与反序列化的过程分别是 encoding 和 decoding
    encoding:把一个Python对象编码转换成Json字符串( json.dumps() )
    decoding:把Json格式字符串解码转换成Python对象( json.loads() )
    对于简单数据类型(string、unicode、int、float、list、tuple、dict),可以直接处理。
    https://docs.python.org/2/library/json.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注