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 applications
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 # 2 # Converts data into a string that is recognized by all programming languages in a special form and writes to the 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---Json