JSON 是用于存储数据的语法。
JSON 是使用 JavaScript.
Python 使用名为 json,调用它来操作 JSON.
import json
您可以使用 json.loads() 读取 Python 中的 JSON 内容。
结果存储在 Python 字典中。
import json
x = '{ name”:patrik”, age”:25, city”:Paris”}'
# 解析器 x:
y = json.loads(x)
print(y[city”])
您可以使用以下方法转换 Python 对象 json.dumps().
import json
# object
x = {name”: patrik”, age”: 25, city”: Paris”}
# 转换为 JSON:
y = json.dumps(x)
# 结果将是 JSON
print(y)
Python 对象可以转换为不同类型的 JSON:
import json
x = {
name”: Adam”,
age”: 32.
single”: False,
children”: (Leo”,Arthur”),
car”: [
{model”: 雷诺”, year”: 2022},
]
}
print(json.dumps(x, indent=4))
Result:
{
name”: 亚当”,
年龄”: 32,
单身”: false,
孩子”: [
L\u00e9o”,
亚瑟”
],
汽车”: [
{
model”: 雷诺”,
year”: 2022
}
]
}
json.dumps(x, indent=4, sort_keys=True)
Please disable your ad blocker and refresh the window to use this website.