Home > Backend Development > Python Tutorial > Explain what you need to pay attention to when operating json in python

Explain what you need to pay attention to when operating json in python

Y2J
Release: 2017-05-11 11:25:43
Original
1342 people have browsed it

This article mainly introduces you to some precautions for string type json operations in python. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it can take a look below.

The methods for python to operate json are

json.dumps——Jsonobject(dictionary) Convert to string object

json.loads——Convert string object to json object (dictionary)

If json object is defined

jsonstring1={"results":[{"id":"1","name":"\u9ed8\u8ba4\u5206\u7ec4","policy":"4","timer_scan_setting":"{\"last\":\"10.29.13\",\"setting\":\"fulldisk\",\"type\":\"day\",\"hour\":\"13\"}"},
{"id":"2","name":"\u6d4b\u8bd5\u7684","policy":"1","timer_scan_setting":"{\"last\":\"10.29.15\",\"setting\":\"fulldisk\",\"type\":\"day\",\"hour\":\"15\"}"},{"id":"4","name":"\u4ea7\u54c1\u7ec4","policy":"3","timer_scan_setting":"{\"last\":\"10.8.15\",\"setting\":\"disable\"}"}]}
Copy after login

You can operate directly according to json, such as

print jsonstring1.keys()
print jsonstring1['results'][0]['policy']
Copy after login

You can also turn it 360 degrees and then operate

jsonstring1=json.dumps(jsonstring1)
jsonstring1=json.loads(jsonstring1)
print jsonstring1.keys()
print jsonstring1['results'][0]['policy']
Copy after login

But be careful if you define a string object

jsonstring2='''{"results":[{"id":"1","name":"\u9ed8\u8ba4\u5206\u7ec4","policy":"4","timer_scan_setting":"{\"last\":\"10.29.13\",\"setting\":\"fulldisk\",\"type\":\"day\",\"hour\":\"13\"}"},
{"id":"2","name":"\u6d4b\u8bd5\u7684","policy":"1","timer_scan_setting":"{\"last\":\"10.29.15\",\"setting\":\"fulldisk\",\"type\":\"day\",\"hour\":\"15\"}"},{"id":"4","name":"\u4ea7\u54c1\u7ec4","policy":"3","timer_scan_setting":"{\"last\":\"10.8.15\",\"setting\":\"disable\"}"}]}'''
Copy after login

This just adds three quotes to the above json object and converts it into a string, so in theory you can directly load and then press json operation

json.loads(jsonstring2)
Copy after login

But in fact, an error is reported, the reason is because of the braces before and after The double quotes are not removed. Many online json formatting tools on the Internet will not report errors for these double quotes, but python will. However, when defining the json object, adding double quotes does not report an error because the content inside will be escaped if not added. , so don’t completely trust the online json format verification tools.

Summary

[Related recommendations]

1. Python Free Video Tutorial

2. Python basic introductory tutorial

3. Python object-oriented video tutorial

The above is the detailed content of Explain what you need to pay attention to when operating json in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template