import json
# 示例数据
data = {
"name": "Alice",
"age": 30,
"is_student": False,
"courses": ["Math", "Science"],
"address": {
"street": "123 Main St",
"city": "Beijing"
}
}
# 将Python对象转换为JSON字符串,并进行格式化输出
formatted_json = json.dumps(data, indent=4, ensure_ascii=False)
# 输出格式化后的JSON字符串
print(formatted_json)
json.dumps()
函数用于将 Python 对象(如字典、列表等)转换为 JSON 字符串。indent=4
参数指定了缩进级别为 4 个空格,使输出的 JSON 字符串更加易读。ensure_ascii=False
参数确保非 ASCII 字符(例如中文字符)能够正确显示,而不是被转义为 Unicode 编码。上一篇:python 生成随机字符串
下一篇:python time模块
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站