JSON (全名: JavaScript Object Notation对象表示法)是一种轻量级的文本数据交换格式, JSON的数据格式其实就是python里面的
字典格式,面可以包含訪括号括起来的数组,也就是python里面的列表。
{
"_id": 1,
"name": "ZJ",
"age": 22,
"date": "2022.08.03"
}
| 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'}
python对象转换成json字符串
#字符串转换json对象
>>> import json
>>> body='{"Text":"you are nice girl"}'
>>> type(body)
<class 'str'>
>>> 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"}'
>>>
将python数据类型转换并保存到json格式的文件内
将json格式的文件中的数据读取并转换为python类型