Python之json文件

{
    "people":[
        {
          "firstName": "Brett",
          "lastName":"McLaughlin"
        },
        {
          "firstName":"Jason",
          "lastName":"Hunter"
        }        
    ]
}

json简介

  • json是一种轻量级的数据交换格式
  • 完全独立于编程语言的文本格式来存储和表示数据
  • 简单和清晰的层次结构使得json成为理想的数据交换语言。易于阅读和编写,易于机器解析和生成,并有效地提升网络传输效率
  • json相比于xml来讲,数据体积小,传输速度快,格式都是压缩的
  • json格式语法上与创建JavaScript对象的代码相同,由于这种相似性,JavaScript程序可以轻松地将json数据转换为JaveScript对象

json方法

  • load
  • loads:将已编码的 JSON 字符串解码为 Python 对象
  • dump
  • dumps:将 Python 对象编码成 JSON 字符串

示例 

#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
原文地址:https://www.cnblogs.com/xiaobingqianrui/p/8513724.html