In fact, JSON is the string representation of a Python dictionary, but a dictionary cannot be passed directly as a complex object, so it needs to be converted to a string. The process of conversion is also a serialization process.
Serialize to JSON string format with Json.dumps
Copy Code code as follows:
>>> Import JSON
>>> dic {' Connection ': [' keep-alive '], ' Host ': [' 127.0.0.1:5000 '], ' cache-control ': [' max-age=0 ']}
>>> jdict = json.dumps ({' Connection ': [' keep-alive '], ' Host ': [' 127.0.0.1:5000 '], ' cache-control ': [' max-age= 0 ']})
>>> Print Jdict
{"Connection": ["keep-alive"], "Host": ["127.0.0.1:5000"], "Cache-control": ["Max-age=0"]}
Although DiC and jdict print the same strings, the actual types are not the same. DIC is a dictionary type, jdict is a string type
Copy Code code as follows:
<type ' Dict ' >
>>> type (jdic)
>>> type (jdict)
<type ' str ' >
You can use the Json.dumps serialization list as a JSON string format
Copy Code code as follows:
>>> list = [1, 4, 3, 2, 5]
>>> jlist = json.dumps (list)
>>> Print JList
[1, 4, 3, 2, 5]
The list and jlist types are also different.
Copy Code code as follows:
>>> type (list)
<type ' list ' >
>>> type (jlist)
<type ' str ' >
Json.dumps has the following various parameters
Copy Code code as follows:
Json.dumps (obj, Skipkeys=false, Ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=None, Separators=none, encoding= "Utf-8", Default=none, Sort_keys=false, **kw)
Key sort
Copy Code code as follows:
>>> Print Json.dumps ({1: ' A ', 4: ' B ', 3: ' C ', 2: ' d ', 5: ' F '},sort_keys=true)
{"1": "A", "2": "D", "3": "C", "4": "B", "5": "F"}
formatting alignment
Copy Code code as follows:
>>> Print json.dumps ({' 4 ': 5, ' 6 ': 7}, Sort_keys=true, indent=4)
{
"4": 5,
"6": 7
}
Specify separator
Copy Code code as follows:
>>> json.dumps ([1,2,3,{' 4 ': 5, ' 6 ': 7}], separators= (', ', ': '))
' [1,2,3,{"4": 5, "6": 7}] '
Serializing to a file object with Json.dump
Copy Code code as follows:
>>> json.dump ({' 4 ': 5, ' 6 ': 7}, open (' Savejson.txt ', ' W '))
>>> Print open (' Savejson.txt '). ReadLines ()
[' {' 4 ': 5, ' 6 ': 7} ']
Json.dump parameters and Json.dumps are similar
Copy Code code as follows:
Json.dump (obj, FP, Skipkeys=false, Ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=None, Separators=none, encoding= "Utf-8", Default=none, Sort_keys=false, **kw)
Json.loads to deserialize a JSON string into a Python object
The function signature is:
Copy Code code as follows:
Json.loads (s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]] ]]])
Note that "s" here must be a string, deserialized to a Unicode character
Copy Code code as follows:
>>> dobj = json.loads (' {' name ': ' AAA ', ' Age ': 18} ')
>>> type (dobj)
<type ' Dict ' >
>>> Print Dobj
{u ' age ': #, U ' name ': U ' aaa '}
Json.load from a file into a Python object
Signed as:
Copy Code code as follows:
Json.load (fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]] ]]])
Instance:
Copy Code code as follows:
>>> fobj = json.load (open (' savejson.txt '))
>>> Print Fobj
{u ' 4 ': 5, U ' 6 ': 7}
>>> type (fobj)
<type ' Dict ' >