In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how python returns json as a client parsing server". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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. However, under the mode of JSON S, the client has to interact with the server, and some of them will use Toutiao to transfer data (for example, Jinri Toutiao).
Acquisition of JSON
The first step is to introduce the requests library, which can simulate a browser to make a request to the server. You can use the get () method of requests to initiate a request to the target url.
Import requestsresponse = requests.get ('http://www.kuaidi100.com/query?type=ems&postid=1111111111111')# uses request to request a json, where the express order number is casually written by the editor
After you get the response, you can get the content of the response (here is a string) through the text () method of requests.
Resolution of response = response.textJSON
The next content is the JSON parsing method mentioned in the manual, mainly the two methods of the json library, loads () and dumps ().
Response = json.loads (response)
# you can encode this string using the loads method of the JSON module
Print (response)
Print (type (response))
# print the result and find it is a dictionary (JSON corresponds to the object of JavaScript, corresponding to the dictionary of python, corresponding to the map of java)
Response = json.dumps (response)
# using the dumps method of JSON, you can convert dictionaries into strings (the transmission of JSON is transmitted as strings,)
Print (response)
Print (type (response))
The complete code import requestsimport jsonresponse = requests.get ('http://www.kuaidi100.com/query?type=ems&postid=111111111111')# requests a json using request The express order number here is print (response) print (type (response)) # printed by the editor and found to be an object response = response.text# using requests's text method to pull out the response text print (response) print (type (response)) # and found to be a string after printing (JSON is transmitted as a string) response = json.loads (response) # using the loads method of JSON module You can encode this string. Print (response) print (type (response)) # prints the result and finds that it is a dictionary (JSON corresponds to the object of JavaScript, corresponding to the dictionary of python, corresponding to the map of java) response = json.dumps (response) # using JSON's dumps method, the dictionary can be converted into a string (the transmission of JSON is transmitted as a string ) print (response) print (type (response)) "how python returns json as a client parsing server" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.