{
"people":[
{
"firstName": "Brett",
"lastName":"McLaughlin"
},
{
"firstName":"Jason",
"lastName":"Hunter"
}
]
}
json简介
- json是一种轻量级的数据交换格式
- 完全独立于编程语言的文本格式来存储和表示数据
- 简单和清晰的层次结构使得json成为理想的数据交换语言。易于阅读和编写,易于机器解析和生成,并有效地提升网络传输效率
- json相比于xml来讲,数据体积小,传输速度快,格式都是压缩的
- json格式语法上与创建JavaScript对象的代码相同,由于这种相似性,JavaScript程序可以轻松地将json数据转换为JaveScript对象
json方法
- load
- loads
- dump
- dumps
示例
#coding = utf-8 import requests import json response = requests.get("http://httpbin.org/get") print(response.json()) strtext = json.loads(response.text) print (type(strtext)) for key,value in strtext.items(): print (key,value) print (strtext["url"])
{‘args‘: {}, ‘headers‘: {‘Accept‘: ‘*/*‘, ‘Accept-Encoding‘: ‘gzip, deflate onnection‘: ‘close‘, ‘Host‘: ‘httpbin.org‘, ‘User-Agent‘: ‘python-requests/ 4‘}, ‘origin‘: ‘111.175.40.89‘, ‘url‘: ‘http://httpbin.org/get‘} <class ‘dict‘> args {} headers {‘Accept‘: ‘*/*‘, ‘Accept-Encoding‘: ‘gzip, deflate‘, ‘Connection‘: se‘, ‘Host‘: ‘httpbin.org‘, ‘User-Agent‘: ‘python-requests/2.18.4‘} origin 111.175.40.89 url http://httpbin.org/get http://httpbin.org/get