- JSON简介
- JSON长啥样
- JSON注意事项
- JSON模块
- 1、json.loads()
- 2、json.dumps()
- 3、json.load()
- 4、json.dump()
JSON (全名: JavaScript Object Notation对象表示法)是一种轻量级的文本数据交换格式, JSON的数据格式其实就是python里面的
字典格式,面可以包含訪括号括起来的数组,也就是python里面的列表。
- JSON独立于语言
- JSON具有自我描述性,更易理解
- JSON比XML更小更快,更易解析
- 爬虫经常经常会获取接数据,接口数据就是JSON格式
{
"_id": 1,
"name": "ZJ",
"age": 22,
"date": "2022.08.03"
}
JSON注意事项
- json的键必须用双引号
- 值中不允许出现function,undefined,Nan,可以使用null
- 结尾不允许出现无意义的逗号
| Python | JSON |
|---|---|
| dict(字典) | object |
| list/tuple(列表/元组) | array |
| str | string |
| int/float/int- & float-derived Enums | number |
| True | true |
| False | false |
| None | null |
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | str |
| number(int) | int |
| number(real) | float |
| true | True |
| false | False |
| null | None |
json字符串转换成python对象
>>> import json
>>> body = {"Text":"You are so beautiful"}
>>> jsonob = json.dumps(body)
>>> jsonob
'{"Text": "You are so beautiful"}'
>>> json.loads(jsonob)
{'Text': 'You are so beautiful'}
2、json.dumps()
python对象转换成json字符串
#字符串转换json对象
>>> import json
>>> body='{"Text":"you are nice girl"}'
>>> type(body)
>>> json.dumps(body)
'"{\"Text\":\"you are nice girl\"}"'
# dict转换为json对象
>>> import json
>>> body = {"Text":"You are so beautiful"}
>>> json.dumps(body)
'{"Text": "You are so beautiful"}'
>>>
3、json.load()
将python数据类型转换并保存到json格式的文件内
4、json.dump()将json格式的文件中的数据读取并转换为python类型



