A Python Hack for a Day (1)

Hi, this is Milo, a blogger who would like to share his test development techniques, interview tips and growing experiences with you!

Welcome everyone to pay attention to my public number: test development pit goods.

Very not ashamed to open a series, this series as far as possible to give you more useful content, especially short, but also pithily enough!

The story

Today, bloggers need to pass a string of JSON data in their work. What does it look like?

The fieldsJson field accepts a string containing escaped JSON.

Probably many people will add \ manually to complete the escape, some people will go to a dedicated site to escape.

So how do you do that in Python?

Use opportunely print

Suppose I have a JSON:

{"name": "miluo"."age": 18."salary": "10k"}
Copy the code

First, it is already a string, enclosed in Python triple quotes.

s = """{"name": "miluo", "age": 18, "salary": "10k"}"""
Copy the code

Then we serialize it again, which I’m sure 100 percent of us can do:

import json
s = """{"name": "miluo", "age": 18, "salary": "10k"}"""
s = json.dumps(s)
Copy the code

Finally we print it out:

print(s)
Copy the code

Look at the results:

The last thing you need to do is copy the contents of print.

Non! Always the case! Good! Use!