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