Python json module instance, pythonjson
In fact, JSON is the string representation of the Python dictionary, but the dictionary cannot be directly transmitted as a complex object, so it needs to be converted to the string form. The conversion process is also a serialization process.
Serialized in json. dumps format
Copy codeThe Code is as follows:
>>> Import json
>>> Dic {'connection': ['keep-alive'], 'host': ['2017. 0.0.1: 5000 '], 'cache-control': ['max-age = 0']}
>>> Jdict = json. dumps ({'connection': ['keep-alive'], 'host': ['123. 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 strings are the same, their types are actually different. dic is the dictionary type and jdict is the string type.
Copy codeThe Code is as follows:
<Type 'dict '>
>>> Type (jdic)
>>> Type (jdict)
<Type 'str'>
Json. dumps serialization list can be used as a json string
Copy codeThe Code is 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 codeThe Code is as follows:
>>> Type (list)
<Type 'LIST'>
>>> Type (jlist)
<Type 'str'>
Json. dumps has the following parameters:
Copy codeThe Code is 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 sorting
Copy codeThe Code is 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 "}
Format alignment
Copy codeThe Code is as follows:
>>> Print json. dumps ({'4': 5, '6': 7}, sort_keys = True, indent = 4)
{
"4": 5,
"6": 7
}
Delimiter
Copy codeThe Code is as follows:
>>> Json. dumps ([1, 2, 3, {'4': 5, '6': 7}], separators = (',',':'))
'[1, 2, 3, {"4": 5, "6": 7}]'
Serialize data to a file object using json. dump
Copy codeThe Code is as follows:
>>> Json. dump ({'4': 5, '6': 7}, open('savejson.txt ', 'w '))
>>> Print open('savejson.txt '). readlines ()
['{"4": 5, "6": 7}']
Json. dump parameters are similar to json. dumps parameters.
Copy codeThe Code is 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 deserializes json strings into python objects
Function signature:
Copy codeThe Code is as follows:
Json. loads (s [, encoding [, cls [, object_hook [, parse_float [, parse_int [, parse_constant [, object_pairs_hook [, ** kw])
Note that the "s" must be a string, which is a unicode Character After deserialization.
Copy codeThe Code is as follows:
>>> Dobj = json. loads ('{"name": "aaa", "age": 18 }')
>>> Type (dobj)
<Type 'dict '>
>>> Print dobj
{U'age': 18, u'name': u'aaa '}
Json. load deserialization from a file into a python object
Signature:
Copy codeThe Code is as follows:
Json. load (fp [, encoding [, cls [, object_hook [, parse_float [, parse_int [, parse_constant [, object_pairs_hook [, ** kw])
Instance:
Copy codeThe Code is as follows:
>>> Fobj = json.load(open('savejson.txt '))
>>> Print fobj
{U '4': 5, u '6': 7}
>>> Type (fobj)
<Type 'dict '>