A---dumps----(JSON string)----loads---B;
OBJ---stringify---(JSON string)----Parse---str; (js at the front)
var obj1={name: "WC", age:18};
var str=json.stringify (OBJ1);
Console.log (str);
var str1= ' {' name ': ' WC ', ' age ': 18} ';
var obj=json.parse (STR1);
Console.log (obj);
Serialization Module
The two modules used in Python for serialization:
- JSON cross-platform cross-language data transfer format for conversion between "string" and "Python Basic data Types"
- Pickle Python's built-in data transfer format, used in binary form, to convert between Python-specific types and Python basic data types
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
#pickle. Dumps converts data through special forms to only Python-recognized strings import pickledata={' K1 ': 123, ' K2 ': ' Hello '}p_str=pickle.dumps (data) print (p_str)------->b ' \x80\x03}q\x00 (x\x02\x00\x00\x00k2q\x01x\x05\x00\x00\x00helloq\x02x\x02\x00\x00\x00k1q\ X03k{u. ' s = pickle.loads (p_str) print (s)-------->{' K2 ': ' Hello ', ' K1 ': 123} #pickle. Dump converts data into characters that only Python knows in a special form String, and write the file with open (' db ', ' W ') as Fp:pickle.dump (DATA,FP) JSON instance #json.loads () #将字符串转换成python基本数据类型, note: Double quotes must be inside, Outside is single quote import jsons= ' {"name": "Tina", "Age": "+"} ' l= ' [1,2,3,4] ' r=json.loads (l) w=json.loads (s) print (R,type (r)) print (W,type (W)) ########### #执行结果如下: ###########[1, 2, 3, 4] <class ' list ' >{' age ': ' + ', ' name ': ' Tina '} <class ' Dict ' > #json. Dumps () Converts the basic data type of Python into a string a={"name": "Tina", "Age": "}b=json.dumps" (a) print (B,type (b)) ############# The results of the execution are as follows: ##########{"age": "+", "name": "Tina"} <class ' str ' > #不带s的是对文件进行操作dic = {' K1 ': 123, ' K2 ': 345}a=json.dump ( Dic,open (' db ', ' W ')) Print (A,type (a)) #读内容 # String converted to dictionary r=json.load (open (' db ', ' R ')) print (R,type (r) ############ #执行结果如下: ######### #写入db文件中的内容即为dict {' K2 ': 345, ' K1 ': 123} <class ' Dict ' >
JSON, pickle