Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python return json to the client or front end as a server?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you how python returns json to the client or the front end as a server. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Introduction

Json as the only connection after the separation of the front and back ends, both the back end and the front end must have a certain ability to parse json, and the back end especially depends on the resolution of json.

Realize

The json here is directly compiled by yourself (the standard program should generate a certain format of json by the program itself, and the json should contain some fixed information, such as response status, etc.). The main ideas are written in the notes, and readers can understand them by line.

From flask import Flaskimport jsonapp = Flask (_ _ name__) @ app.route ('/ hello') # specifies url and executes the annotated function def hello_world () when the requested url is / hello: data = {'no': 1,' name': 'W3C schoolrooms,' url': 'http://www.yisu.com'} # in python, the data format corresponding to json is a dictionary So here we create a dictionary to store data and return print (type (data)) # print, make sure that the data type is the dictionary json_str = json.dumps (data) # use dumps to convert the dictionary type to a string, so that it can be returned through the http protocol # json is transmitted as a string return json_str # using flask You can return this string directly with return, and you can return json to if _ _ name__ = ='_ _ main__': app.run () # to run this flask project. This is all the content of the article "how python returns json to the client or front end as a server. Thank you for reading!" I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report