python flask json
时间: 2023-10-18 09:06:51 浏览: 111
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
}
```
阅读全文
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)