python json转字符串
时间: 2023-09-25 15:07:13 浏览: 113
要将Python对象转换为JSON字符串,可以使用json.dumps()函数。例如,如果有一个Python字典对象,你可以使用json.dumps()将其转换为JSON字符串。以下是一个示例代码:
import json
data = {"name": "John", "age": 30, "city": "New York"}
json_string = json.dumps(data)
print(json_string)
# 输出:{"name": "John", "age": 30, "city": "New York"}
使用json.dumps()函数可以将Python对象转换为符合JSON格式的字符串。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python 对象与 JSON 字符串的相互转换](https://blog.csdn.net/qq_44214671/article/details/115622742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文