JSON (JavaScript Object Notation) is a lightweight data format used to exchange data between a client and a server. In Python, you can use the built-in json module to work with JSON data.

Here's an example of how to create a JSON response in Python:

import json
from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/example')
def example():
    # Create a Python dictionary
    data = {'name': 'John', 'age': 30, 'city': 'New York'}

    # Convert the dictionary to JSON
    json_data = json.dumps(data)

    # Return the JSON response
    return jsonify(json_data)

if __name__ == '__main__':
    app.run()

In this example, we're using the Flask web framework to create a simple endpoint that returns a JSON response. We're creating a Python dictionary, converting it to JSON using json.dumps(), and returning the JSON data using Flask's jsonify() method.

Note that when using jsonify(), you don't need to manually set the Content-Type header to application/json - Flask takes care of that for you.

python response json

原文地址: https://www.cveoy.top/t/topic/Id7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录