Concept:
serialization (serialization): Converts the state information of an object into a process that can be stored or transmitted over a network, in a format that can be json,xml, and so on. Deserialization is the state of the deserialized object that is read from the storage area (Json,xml) and re-created.
JSON(Java Script Object Notation): A lightweight data interaction format that is easier to read and write than XML, is easy to parse and generate, and JSON is a subset of JavaScript.
The python2.6 version begins with the JSON module, and the Python JSON module serialization and deserialization process is encoding and decoding, respectively.
- Encoding: Converts a Python object encoding into a JSON string.
- Decoding: Converts the JSON format string encoding into a Python object.
Specific application:
JSON offers four features: dumps, dump, loads, load
1 #dumps function2 #converts data into a string that is recognized by all programming languages in a special form3>>>ImportJSON4>>> data = ['AA','BB','cc']5>>> J_str =json.dumps (data)6>>>J_str7 '["AA", "BB", "CC"]'
1 #loads function2 #Convert a JSON-encoded string into a Python data structure3>>>J_str4 '["AA", "BB", "CC"]'5>>> mes =json.loads (J_STR)6>>>mes7['AA','BB','cc']
1 # Dump function 2 # converts data into a string that is recognized by all programming languages in a special form and writes to a file 3 with open ('d:/tmp.json'w') as F: 4 json.dump (data, F)
1 # Load Function 2 # reading data from a data file and converting a JSON-encoded string into a Python data structure 3 with open ('d:/tmp.json'R') as F: 4 data = Json.load (f)
Description:
The basic types supported by JSON encoding are: None, bool, int, float, string, list, tuple, Dict.
For dictionaries, JSON assumes that key is a string (any non-string key in the dictionary is converted to a string at encoding), and to conform to the JSON specification, only Python lists and dictionaries should be encoded. In addition, it is a standard practice to define the topmost object as a dictionary in a Web application.
The JSON-encoded format is almost identical to the Python syntax, slightly different: True will be mapped to True,false will be mapped to False,none will be mapped to null, tuple () will be mapped to list [], because other languages do not have the concept of tuples, only arrays, This is the list.
1>>>ImportJSON2>>> data = {'a': True,'b': False,'C': None,'D':(), 1:'ABC'}3>>> J_str =json.dumps (data)4>>>J_str5 '{"A": true, "C": null, "D": [1, 2], "B": false, "1": "ABC"}'
Python's JSON module