This article mainly introduces the example of using the Pythonjson module. This article provides multiple code examples. For more information, see JSON, which is a string representation of the Python dictionary, however, the dictionary cannot be directly transmitted as a complex object, so it must be converted into a string. the conversion process is also a serialization process.
Serialized in json. dumps format
The 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.
The code is as follows:
>>> Type (jdic)
>>> Type (jdict)
Json. dumps Serialization list can be used as a json string
The 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.
The code is as follows:
>>> Type (list)
>>> Type (jlist)
Json. dumps has the following parameters:
The 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
The 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
The code is as follows:
>>> Print json. dumps ({'4': 5, '6': 7}, sort_keys = True, indent = 4)
{
"4": 5,
"6": 7
}
Delimiter
The 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
The 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.
The 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:
The 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.
The code is as follows:
>>> Dobj = json. loads ('{"name": "aaa", "age": 18 }')
>>> Type (dobj)
>>> Print dobj
{U'age': 18, u'name': u'aaa '}
Json. load deserialization from a file into a python object
Signature:
The code is as follows:
Json. load (fp [, encoding [, cls [, object_hook [, parse_float [, parse_int [, parse_constant [, object_pairs_hook [, ** kw])
Instance:
The code is as follows:
>>> Fobj = json.load(open('savejson.txt '))
>>> Print fobj
{U '4': 5, u '6': 7}
>>> Type (fobj)