In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces you how to convert python str and json types, the content is very detailed, interested friends can refer to, hope to be helpful to you.
When writing the interface test framework. Data type conversions can not be avoided, such as casting string types, such as converting json types
Str to json
To convert python strings to json objects, you need to use the loads function of the json module
Import json str ='{"accessToken": "521de21161b23988173e6f7f48f9ee96e28" "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"}'j = json.loads (str) print (j) print (type (j)) 1 2 3 4 5 6 7
Output
{'accessToken':' 521de21161b23988173e6f7f48f9ee96e288, 'User-Agent':' Apache-HttpClient/4.5.2 (Java/1.8.0_131)'} 1 2 json to strimport json j = {"accessToken": "521de21161b23988173e6f7f48f9ee96e28" User-Agent: "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"} str = json.dumps (j) print (str) print (type (str)) 1 2 3 4 5 6 7
Output
{"accessToken": "521de21161b23988173e6f7f48f9ee96e28", "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_131)"} 1 2 questions
The main purpose of this article is to solve a problem with mark. When converting str to json, the problem of quotation marks in str format leads to failure and error report.
Look at the following code
Import json str = "{'accessToken':' 521de21161b23988173e6f7f48f9ee96e28' 'User-Agent':' Apache-HttpClient/4.5.2 (Java/1.8.0_131)'} "j = json.loads (str) print (j) print (type (j)) 1 2 3 4 5 6 7
At first glance, there was no problem, but there was a mistake.
Json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) 1
Why?
In the string, the double quotation marks are in the periphery and the single quotation marks are embedded, causing the conversion to fail
On how to carry out python str and json type conversion to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.