python flask json
时间: 2023-10-18 07:06:51 浏览: 38
Python Flask is a web framework for building web applications using Python. JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
In Flask, we can use the jsonify function to convert Python objects to JSON format. Here's an example:
```python
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/example')
def example():
data = {
'name': 'John',
'age': 25,
'is_student': True
}
return jsonify(data)
if __name__ == '__main__':
app.run()
```
In this example, we define a route `/example` that returns a JSON object containing a name, age, and whether or not the person is a student. We use the `jsonify` function to convert the Python dictionary to JSON format.
When we run the Flask application and navigate to `http://localhost:5000/example`, we should see the JSON object displayed in the browser:
```
{
"name": "John",
"age": 25,
"is_student": true
}
```