python 解析json

#coding:utf-8
import json
'''
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。JSON采用完全独立于语言的文本格式,这些特性使JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成。

JSON建构于两种结构:
1. “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),记录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
2. 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
3、值可以是字符串、数字、true、false、null,也可以是对象或数组。这些结构都能嵌套。
'''
#编码:把一个Python对象编码转换成Json字符串   json.dumps()
python_text={'id':'10001',
             'name':'aaa',
             'skill':['bbb','ccc','ddd']}
json_text=json.dumps(python_text)
print type(json_text)

#解码:把Json格式字符串解码转换成Python对象   json.loads()
json_text='[{"topic_id":"00493","log_time":"2016-03-23 11:09:35","id":"2016_03_235587540165083004930","ext":"","processed_time":0,"ret_status":-1,"err_msg":"more than access restrictions"},{"topic_id":"600000","log_time":"2016-03-23 11:14:45","id":"2016_03_230","ext":"total:9(query:9/merge:0/xml:0/depth:1) total_cnt:0 debug:time:8(switch:6/lable:2/data:0) count:0/0","processed_time":12,"ret_status":0,"err_msg":""},{"topic_id":"10396011521349","log_time":"2016-03-23 11:14:10","id":"2016_03_235587541315832250405","ext":"","processed_time":0,"ret_status":-1,"err_msg":"more than access restrictions"},{"topic_id":"0000039","log_time":"2016-03-23 11:14:02","id":"2016_03_235587541148747956235","ext":"total:244(query:118/merge:9/xml:117/depth:1) total_cnt:74 debug:time:117(switch:6/lable:8/data:103) count:74/74","processed_time":248,"ret_status":0,"err_msg":""}]'
python_text=json.loads(json_text)
print type(python_text)

#dumps方法的可选参数-排序
data1 = {'b':789,'c':456,'a':123}
data2 = {'a':123,'b':789,'c':456}
d1 = json.dumps(data1,sort_keys=True)
d2 = json.dumps(data2)
d3 = json.dumps(data2,sort_keys=True)
print d1
print d2
print d3
print data1==data2,d1==d2,data1==d1 #dic无序,json有序
print type(data1),type(d1)

#缩进
d4=json.dumps(data1,sort_keys=True,indent=4)
print d4

#skipkeys略过非string类型的key
data= [ { 'a':'A', 'b':(2, 4), 'c':3.0, ('d',):'D tuple' } ]

try:
    print json.dumps(data)
except (TypeError, ValueError) as err:
    print 'ERROR:', err
print 
print json.dumps(data, skipkeys=True)

#loads方法
print '*****'
#获取json数据的途径可以是下载网页和数据库查询
import urllib2
url='http://api.douban.com/v2/book/isbn/9787218087351'
request=urllib2.Request(url)
page=urllib2.urlopen(request)
data=page.read() #现在是字符串格式
j_data=json.loads(data) #转化为Python格式
#通过JsonView搞清楚嵌套关系,就可以取到想要的数据
print j_data['rating']
print j_data['images']['large']
print j_data['summary']

#json格式字符串(大文件)写到文件流中
import tempfile

f = tempfile.NamedTemporaryFile(mode='w+')
f.write('[{"a": "A", "c": 3.0, "b": [2, 4]}]')
f.flush()
f.seek(0)

print json.load(f)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风起云永

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值