python 中jsonify得到的数据是什么格式列子
jsonify 函数在 Flask 中用于将 Python 对象转换为 JSON 格式的响应。转换后的数据格式为字符串。以下是示例:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def hello_world():
data = {
"name": "John",
"age": 30,
"isMarried": True,
"hobbies": ["reading", "coding", "playing"],
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
}
}
return jsonify(data)
if __name__ == '__main__':
app.run()
当我们访问 http://localhost:5000/ 时,会得到以下响应:
{
"name": "John",
"age": 30,
"isMarried": true,
"hobbies": ["reading", "coding", "playing"],
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY"
}
}
可以看到,响应的数据格式为 JSON 格式的字符串
原文地址: https://www.cveoy.top/t/topic/fkSR 著作权归作者所有。请勿转载和采集!