Python之json文件

Posted Fate0729

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python之json文件相关的知识,希望对你有一定的参考价值。

{
    "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

 

以上是关于Python之json文件的主要内容,如果未能解决你的问题,请参考以下文章

python之模块和包

vscode之快速生成vue模板

Python 之 Json模块使用

python之基础篇——模块与包

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

Python3之json文件操作