JSON (JavaScript Object Notation, JS Object Shortcut) is a lightweight data exchange format. It is based on a subset of ECMAScript (the js specification developed by the European Computer Society) and uses a text format to store and represent data that is completely independent of the programming language. The simplicity and clarity of the hierarchical structure make JSON an ideal language for data exchange. It is easy for people to read and write, as well as easy for machines to parse and generate, and effectively improves the efficiency of network transmission.
The following article gives you an introduction to python in the json, take a look at it together
JSON
JSON(Java Script Object Notation)
To use json you must import the json library:import json
dumps() json formatting conversion Dictionary to string conversion
>>> import json >>> data = {'a':1,'b':2,'c':3} >>> (data) '{"a": 1, "b": 2, "c": 3}'
indent to specify the number of indents
>>>(data,indent=4) //4estimated number
separators() function for the element separators, object key value separators (the purpose of the json file to become more compact)
>>>(data,separators(',',':')) {"a":1,"b":[1,2,3],"c":3}
ensure_ascii() when there is a Chinese character is in the dictionary (ehsure_ascii = False) to solve the messy code problem (json defaults to ascii code).
>>>s = (data,nsure_ascii=False)
dump() json write to file
>>> with open(r'C:\Users\','w') as f: ... (data,f) ... >>>with open(r'C:\Users\','r') as f: ... () ... '{"a": 1, "b": 2, "c": 3}'
loads() converts a json file to a dictionary
>>> s = (data) >>> s '{"a": 1, "b": 2, "c": 3}' >>> (s) {'a': 1, 'b': 2, 'c': 3} >>>
load() reads json data from a file
>>> with open(r'C:\Users\gallo\Desktop\python\little game\practice\','r') as f: ... (f) ... {'a': 1, 'b': 2, 'c': 3} >>>
summarize
The above is a summary of json in python, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. I would also like to thank you very much for your support of my website!