Serialization and de-serialization python tuples, dictionaries, lists of

  In python, the serialized object coding means Python string converted to JSON format; deserialized is the opposite, the string is decoded Python JSON format as data objects. Which specializes in the JSON library to handle this process.

  A, JSON library application

  After the built-in data structures such as tuples, dictionaries, processing sequence listing, type str (string), and after deserialization processing pass, the data structure is a list of still and dictionaries (tuple serialized and deserialized after the data type of dictionary, no longer tuple). In JSON library, serialization and deserialization process is divided into two parts, the processing part specific list data, the other is processing of the contents of the file.

1, examples of the conversion code is as follows:

# - * - Coding: UTF-. 8 - * - 
# python3.6
Import JSON # list List1 = [1,2,3,4,5] Print (List1) Print ( "the list of sequences and deserialization processing: ") Print (" not performed before the sequence listing data types: ", type (List1)) # list serializing list_str = json.dumps (List1) Print (" contents list is serialized : {0}, type: {}. 1. "the format (list_str, type (list_str))) # string list_str deserialize a str_list = json.loads (list_str) Print (" string after deserialization content: {0}, type: {. 1}. "the format (a str_list, type (a str_list))) # tuple tuple1 = ( 'name', 'huzi', 'Age') Print (tuple1) Print (" tuple serialization and deserialization process: ") print (" tuple not previously serialized data types: ", type (tuple1)) # tuple tuple1 serializing tuple_str = json.dumps(tuple1) print ( "contents tuple1 sequence of tuples is: {0}, type: {}. 1." the format (tuple_str, type (tuple_str))) # string tuple_str deserialization process str_tuple = json.loads (tuple_str) Print ( "contents deserialization tuple_str string is: {0}, type: {}. 1" the format (str_tuple, type (str_tuple)).) # dictionary dict1 = { 'name': ' huzi ',' Age ':' 22 is'} Print (dict1) Print ( "dictionary serialization and deserialization process") Print ( "before the dictionary is not serialized data types:", type (dict1)) # dictionary dict1 serializing dict_str = json.dumps (dict1) Print ( "the contents of the dictionary dict1 sequence is: {0}, type: {1}". format ( dict_str, type (dict_str))) # string dict_str deserialized treatment str_dict = json.loads (dict_str) Print ( "the contents of the string dict_str sequence is: {0}, type: {1}". format ( str_dict, type (str_dict )))

Print results are as follows:

  2, examples of the application code as follows:

  在实际服务端与客户端的数据交互中,客户端发送请求到服务端,服务端响应回复数据给客户端,客户端拿到响应数据后就把这些数据存储在文件中了 ,然后后续再继续对文件的数据进行处理,在处理的过程中需要进行序列化与反序列化处理,而序列化的过程实际上是把数据存储在文件中的过程,反序列化的过程是读取文件里边的内容的过程。记录一个例子,例子内容为把服务端返回的数据存储在文件中,然后再读取文件的内容

"""
 请求接口为  http://**9.*7.**.**3:8000/login ,
 把服务端返回的响应数据存储在文件中,然后对文件反序列化处理获取里面的具体的值
"""
import requests import json def login(): headers = { 'Content-Type': 'application/json'} data = {"userName":"11111111112","password":"123456"} response = requests.post( url = 'http://**9.*7.**.**3:8000/login', json = data, headers = headers ) versionInfo = response.text print("返回的参数未序列化之前的数据类型为:{0},内容为 {1}".format(type(versionInfo),versionInfo)) # 把服务端返回的响应数据序列化并存储在文件中 json.dump(response.json(),open('json.md','w')) login() # 对文件进行反序列化处理,取值message对应的value具体值 dict1 = json.load(open('json.md','r')) print("文件数据反序列化后的类型为:{0},内容为:{1}".format(type(dict1),dict1)) print("message对应的value的值为:{0}".format(dict1['message']))

  打印结果为:

 

Guess you like

Origin www.cnblogs.com/Zhan-W/p/11260220.html