json, pickle in python

json provides four functions:

  • dumps : convert json data to string
    json.dumps(obj,skipkeys=False,separators=None,sort_keys=False)
  • loads loads strings as json data
    json.loads(s)
  • dump : serialize json data to file
    json.dump(obj, fp, sort_keys=False,)
  • load : Loading json data from a file
    json.load(fp)
    omits many formal parameters. For more details, see the official json documentation

pickle also provides four similar functions:

  • pickle.dump(obj, file, protocol=None, *, fix_imports=True)
  • pickle.dumps(obj, protocol=None, *, fix_imports=True)
  • pickle.load(file, *, fix_imports=True, encoding=”ASCII”, errors=”strict”)
  • pickle.loads(bytes_object, *, fix_imports=True, encoding=”ASCII”, errors=”strict”)

    For more details, see the official documentation of pickle
    . The difference is that json implements the conversion between json data and strings, while pickle implements the conversion between json data and byte objects.

    The following figure shows an example of pickle:
    pickle
    Another important difference between json and pickle is that json can only serialize the basic data types of python : Number (number) String (string) List (list) Tuple (tuple) Sets (collection) Dictionary (dictionary), but cannot serialize classes, objects, etc.; and pickle can serialize everything , including classes, objects, functions, and so on.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518761&siteId=291194637