Preface
effect
loads,load Usage of
dumps,dump Usage of
Conclusion
Prefacejson, Its full name is JavaScript Object Notation, That is to say JavaScript Object tag , Data is represented by a combination of objects and arrays , Although the structure is simple, the degree of structure is very high , Is a lightweight data exchange format .
effectMainly used to python Object code is json Format output or storage , And will be json Format object decoded as python object .
One JSON Objects can be written in the following form :
[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]
Surrounded by brackets is equivalent to list type , Each element in the list can be of any type , In this case it's a dictionary type , Surrounded by braces .
JSON It can be freely combined from the above two forms , You can nest... Infinitely , The structure is clear , It's a great way to exchange data .
Let's take a look at json in loads, and load
loads,load Usage offor example , There is a passage JSON String of form , It is str type , We use it json.loads convert to python Data structure of , Become a list or dictionary , So we can operate .
import jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# Print data type print(type(data))# json Type of data is converted to python Data of type new_data = json.loads(data)# Print data type print(type(new_data))
The operation results are as follows
In this way, we can use the index to get the corresponding content , For example, you want to get... In the first element name attribute , You can use the following methods :
import jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# Print data type print(type(data))# json Type of data is converted to python Data of type new_data = json.loads(data)# Get content name = new_data[0]['name']new_name = new_data[0].get('name')# Print after conversion data type print(type(new_data))print(name)print(new_name)
Running results
That's all loads Usage of .
load The usage of is to put json Format file , convert to python Data of type .
Be careful :load Method operates on the entire file object , Here is to convert the contents of the whole file object into json object .( The following figure shows the operation object )
Example
import jsonimport json# load The usage of is to put json Format file , convert to python Data of type .# The file object that builds the file with open('test1.json',encoding='utf-8')as fp: # Load the long file object , Convert to python Data of type pyth_list = json.load(fp) print(pyth_list) print(type(pyth_list)) print(type(pyth_list[0]))
A file operation object running result is required
That's all loads and load Usage of , Both methods can be used in appropriate scenarios .
dumps,dump Usage ofjson.dumps() function , hold python Type of data to json character string
json.dump() function , hold python Type of data to json Save format to file
dumps function
import jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# Print coming data type print(type(data))# json Type of data is converted to python Data of type new_data = json.loads(data)# hold python Type of data to json character string lit = json.dumps(new_data)# Print after conversion data type print(type(new_data))print(type(lit))
Running results
Actually loads and dumps The usage of is consistent ,loads Yes, it will json Type of data is converted to python Data of type , and dumps Yes, it will json Type of data is converted to python Data of type . One is encoding and the other is decoding .
dump function
hold python Type of data to json Save format to file
import jsonimport jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# json Type of data is converted to python Data of type new_data = json.loads(data)# hold python Type of data to json Save format to file # Build the object to write to the file with open('test1.json','w',encoding='utf-8')as fp: # hold python Type of data to json Save format to file json.dump(new_data,fp,ensure_ascii=False)
In order to output Chinese , You also need to specify parameters ensure_ascii by False
Save files
ConclusionThis is about python Standard library module json This is the end of the article on the basic usage of the library , More about python Standard library module json Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !