What is JSON:
JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.
JSON is constructed in two structures:
A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array).
The sequence of values (an ordered list of values). In most languages, it is understood as an array (array).
These are common data structures. In fact, most of the modern computer languages support them in some form. This makes it possible for a data format to be exchanged between programming languages that are also based on these constructs.
Four ways to use JSON dumps, dump, loads, load
Encode simple data types using a simple Json.dumps method
defTest ():ImportJSON Info= [{'a':'1','b':'2','C':'3','D':'4','F':'5'}] Data= Json.dumps (info, sort_keys=true, indent=4, separators= (',',': ')) Print(data)
def test1 (): Import JSON ' {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5} ' ; = json.loads (jsondata) print(jsondata) print(text)
How to save a JSON-formatted file
Def write_file (): import json a = {' A ': ' 1 ', ' B ': ' 2 ', ' C ': ' 3 '} with open ("Test.json", "w", encoding= ' Utf-8 ') As F: F.write (Json.dumps (A, indent=4)) # #indent Indent 4
How to open JSON file to convert Python
Def open_file (): import JSON with open ("Test.json", "R", encoding= ' Utf-8 ') as f: json_file = Json.loads ( F.read ()) f.seek (0) print (json_file[' B ') return json_file
Using load to open JSON file mode
Def load_file (): import JSON with open ("Test.json", "R", encoding= ' Utf-8 ') as f: json_file = Json.load (f ) F.seek (0) print (json_file[' a ') return json_file
Gets the value of the dictionary
The result of the output shows that the simple type is very similar to its original repr () output after encode, but some data types have changed, for example, the tuple in the previous example is converted to a list. During the encoding process of JSON, there is a conversion process from the original Python type to the JSON type, with the specific conversions as follows:
The Json.dumps () method returns a Str object Encodedjson, and we then decode the Encodedjson to get the raw data and the json.loads () function that we need to use:
Def test2 (): import json obj = [[1,2,3],123,123.123, ' abc ', {' key1 ':(, ' key2 ':(4,5,6)}] Encodedjson = Json.dumps (obj) Decodejson = Json.loads (Encodedjson) print (repr (obj)) print (Encodedjson) Print (decodejson[4][' key1 ')) print (Decodejson)
Comparison of two parameters
In the above example, the data1 and data2 data should be the same, but because of the unordered nature of dict storage, they cannot be compared. Therefore, the two can be sorted by the results of storage to avoid the inconsistent data of the situation occurs, but after sorting and then storage, the system must do more things, it will certainly result in a certain amount of performance consumption, so appropriate sequencing is very important
Def sort_json (): import json data2 = {' B ': 789, ' C ': 456, ' a ': 123} data1 = {' A ': 123, ' B ': 789, ' C ': 456} d1 = Json.dumps (data1, sort_keys=true) #转换时进行排序 d2 = Json.dumps (data2) d3 = Json.dumps (data2,sort_keys=true) Print (D1) print (D2) print ( d3) print ( D1 = = D2) print (D1 = = D3)
Python transforms using JSON format