JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (typically used to improve network transfer rates).
JSON is made up of lists and dict, respectively, in Python.
This is the two modules used for serialization:
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
The JSON dumps converts the data type to a string dump to convert the data type into a string and store it in a file loads convert the string to a data type load opens the file to a data type from a string
JSON is the ability to exchange data between different languages, and pickle is used only between Python. JSON can only serialize the most basic data types, JOSN can only serialize the commonly used data types (list, dictionary, list, string, number,), such as date format, class Object! Josn will die. Pickle can serialize all data types, including classes, and functions can be serialized.
Case:
Dumps: Converting a dictionary from Python to a string
Import jsontest_dict = {' Bigberg ': [7600, {1: [' IPhone ', 6300], [' Bike ', ' + ', ' shirt ', 300]]}]}print (test_dict) print (t Ype (test_dict)) #dumps convert data to String json_str = Json.dumps (test_dict) print (JSON_STR) print (Type (JSON_STR))
Loads: Converting a string to a dictionary
New_dict = Json.loads (json_str) print (new_dict) print (Type (new_dict))
Dump: Writing data to a JSON file
With open (".. /config/record.json "," W ") as F:json.dump (new_dict,f) print (" Load in File Complete ... ")
Load: Opens the file and transforms the string into a data type
With open (".. /config/record.json ", ' R ') as Load_f:load_dict = Json.load (load_f) print (load_dict) load_dict[' Smallberg '] = [8200,{1:[ [' Python ', 81],[' shirt ', 300]]}]print (load_dict) with open (".. /config/record.json "," W ") as Dump_f:json.dump (Load_dict,dump_f)