The basic features are explained first:
Dumps is converting dict to str format, loads is converting str to dict format.
Dump and load are similar functions, just combined with file operations.
Look at the code example:
In [1]:ImportJSON in [2]: a = {'name':'Wang',' Age': 29} in [3]: b =Json.dumps (a) in [4]:PrintB, type (b) {" Age": 29,"name":"Wang"} <type'Str'>In [11]: json.loads (b) out[All]: {u' Age':, U'name': U'Wang'} in [12]:Printtype (json.loads (b))<type'Dict'>
Then look at the difference between dump and dumps, see Code:
In [1]:ImportJSON in [2]: a = {'name':'Wang',' Age': 29} in [3]: b =Json.dumps (a) in [4]:PrintB, type (b) {" Age": 29,"name":"Wang"} <type'Str'>In [5]: c =Json.dump (a)---------------------------------------------------------------------------TypeError Traceback (most recent)<ipython-input-5-92dc0d929363>inch<module>()----> 1 c =Json.dump (a) typeerror:dump () takes at least2 arguments (1 given)
Simply put, dump requires a parameter similar to a file pointer (not a real pointer, which can be called a class file object), which can be combined with a file operation, which means that the dict can be converted to STR and then deposited into a file, whereas dumps directly gives STR, which is the dictionary to str.
See the code for an example (note some small details of the file operation):
In [1]:ImportJSON in [2]: a = {'name':'Wang'} in [3]: fp = file ('Test.txt','W') in [4]: Type (FP) out[4]: file in [5]: Json.dump (A, FP) in [6]: Cat test.txt in [7]: Fp.close () in [8]: Cat test.txt{"name":"Wang"}in [9]: json.load (FP)---------------------------------------------------------------------------ValueError Traceback (most recent)<ipython-input-9-0064dabedb17>inch<module>()----> 1json.load (FP)/usr/local/cellar/python/2.7.11/frameworks/python.framework/versions/2.7/lib/python2.7/json/__init__. PYcinchLoad (FP, encoding, CLS, Object_hook, Parse_float, Parse_int, Parse_constant, Object_pairs_hook, * *kw)285 286"""-- 287 return loads (Fp.read (), 288 encoding=encoding, Cls=cls, Object_hook=object_hook, 289 Parse_float=parse_float, Parse_int=parse_int, valueerror:i/o operation on closed file in [ten]: fp = file (' Test.txt ', ' R ') in [all]: Json.load (FP) out[11]: {u ' name ': U ' Wang '}
Transfer from http://www.cnblogs.com/xuguanghuiblog/p/8630824.html
About the JSON dump and dumps