JSON and AJAX with Python

JSON and AJAX with Python

本文关键字:Python with AJAX and JSON      更新时间:2023-09-26

我正在使用Python/Django向客户端提交一些JSON。我是否必须解析它,或者我可以像调用任何其他对象一样开始调用它的东西?

您可以使用

json模块从 json 字符串转换为 Python 字典:

import json
json_str = '{"foo": "bar"}'
d = json.loads(json_str) # d = {'foo': 'bar'}

或者从 Python 对象到 json 字符串:

import json
d = [1, 2, 3, 4]
json_str = json.dumps(d) # json_str = "[1, 2, 3, 4]"