Json
concept:JSON is a lightweight data interchange format.
If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.
format: an unordered collection of ' name '/' values '. {Name 1/value, name 2/value ...}.
Description: You may be reminded of where this format has been seen, yes. In Python, dictionaries also have similar key:value structures. But you can't confuse it.
- The JSON key can only be a string, and Python's dict could be any hash object
- The JSON key can be ordered.
- The JSON key can be repeated
- The values of JSON can only be strings, floating-point numbers, Boolean values, or null, or arrays or objects that they make up.
Encoding-Serialization:
- Dumps: Converting data types to strings
- Dump: Converts a data type into a string and stores it in a file
Decode--Deserialization:
- Loads: Converting a string to a data type
- Load: Convert file to string from data type
Note: All variables are in memory during the program's run. Once the program is finished, the variables will disappear.
So, the process of turning a variable from memory into a storage or transfer is called serialization. in turn, re-reading the variable contents from the serialized object into memory is called deserialization.
1. Dumps: Convert dictionary to string
in [+]: d=dict (name='xjm') in [101]: a=json.dumps (d) in [ 102]: aout[102'{"name": "XJM"}'in[ 103 ]: Type (a) out[103]: Str
2. Dump: Writing to the JSON file
In [MAX]: with open ('desktop/j.txt','w') as F: ...: a=json.dump (d,f) ... :print(' file write complete ... ' ) ...: File write complete ...
3. Loads: Read JSON file
In [the]: d=json.loads (a) in [+]: dout[]: {'name'XJM '}
4. Load: Read JSON file in file
in [+]: with open ('j.txt','rb') as F: ...: a =json.load (f) ... : in [121]: aout[121]: {'name' ' XJM '}
Python's summary of JSON operations (i)