JSON-related concepts:
Serialization (serialization): Converts the state information of an object into a process that can be stored or transmitted over a network, and the format of the transmission can be json,xml. Deserialization reads the state of the deserialized object from the storage area (Json,xml) and creates the object again.
JSON (Java Script Object notation): A lightweight data interchange format that is simpler and easier to read and write than XML, and is easy to parse and generate, and JSON is a subset of JavaScript.
The python2.6 version begins with the JSON module, and Python's JSON module serializes and deserializes the process of 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
Dumps function: Converts data to a string that is known to all program languages in a special form
>>> Import Simplejson
>>> data =[' AA ', ' BB ', ' cc ']
>>> j_str = simplejsondumps (data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
nameerror:name ' simplejsondumps ' are not defined
>>> j_s TR = simplejson.dumps (data)
>>> j_str
' ["AA", "BB", "CC"] '
Loads function: Convert JSON-encoded strings to Python data structures
>>> mes = simplejson.load (j_str)
Traceback (most recent call last):
File "<stdin>", line 1, in < module>
File "D:\Program files\python\lib\site-packages\simplejson\__init__.py", line 455, in Load
Return loads (Fp.read (),
attributeerror: ' str ' object has no attribute ' read '
>>> mes = Simplejson.loads (j_str)
>>> mes
[' aa ', ' BB ', ' CC ']
1 # Dump function
2 # Converts data to a string that is known to all program languages in a special form and writes file
3 with open (' D:/tmp.json ', ' W ') as F:
4 Simplejson.dump (Data, F)
1 # Load Function
2 # reads data from the data file and converts the JSON-encoded string to Python's data structure
3 with open (' D:/tmp.json ', ' R ') as F:
4 data = Simpl Ejson.load (f)
Description:
The basic types of JSON encoding support are: None, bool, int, float, string, list, tuple, Dict.
For a dictionary, JSON assumes that the key is a string (any non-string key in the dictionary is converted to a string when encoded) and that the JSON specification should be encoded only for Python lists and dictionaries. In addition, in Web applications, it is a standard practice to define top-level objects as dictionaries.
The JSON-encoded format is almost consistent with the python syntax, and slightly different: True will be mapped to True,false to be mapped to null and the tuple () will be mapped to list [] because other languages have no tuple concept, only arrays, Which is the list.
1 >>> Import Simplejson
2 >>> data = {' A ': True, ' B ': False, ' C ': None, ' d ':(1,2), 1: ' ABC '}
3 >& gt;> j_str = simplejson.dumps (data)
4 >>> j_str
5 ' {"A": true, "C": null, "D": [1, 2], "B": false, "1" : "ABC"} '
Simpeljson Module Installation
Development environment: WINDOWS10, Python3.5, DJango1.11.1
The first step: first, download the corresponding Simplejson. whl file, download address: http://www.lfd.uci.edu/~gohlke/pythonlibs/#simplejson
Step two: Open cmd and go to the Scripts folder in the Python installation directory. For example: D:\Program files\python\scripts. Use the PIP to install the WHL file that you just downloaded, pip.exe install *.WHL, for example:
D:\Program Files\python\scripts>pip.exe Install D:\PYTHON\SIMPLEJSON-3.10.0-CP36-CP36M-WIN_AMD64.WHL
Processing D:\PYTHON\SIMPLEJSON-3.10.0-CP36-CP36M-WIN_AMD64.WHL
Installing collected Packages:simplejson
Successfully installed simplejson-3.10.0
Tip After the installation is successful, you can see the Simplejson in the \python\lib\site-packages directory.