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 to convert python str to json types

2025-02-28 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.

Share To

Internet Technology

Wechat

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

12
Report