温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Python怎么实现Json的序列化与反序列化

发布时间:2021-04-08 18:00:19 来源:亿速云 阅读:158 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关使用Python怎么实现Json的序列化与反序列化,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1、Json序列化如下:

import json
print (json.__all__)  #查看json库的所有方法
['dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONEncoder']

未在dumps函数中添加参数ensure_ascii=False,结果如下:

#coding: utf-8
import json
dict = {'name':'zhangsan', 'age':33, 'address':'红星路'}
print('未序列化前的数据类型为:', type(dict))
print('为序列化前的数据:', dict)
#对dict进行序列化的处理
dict_xu = json.dumps(dict)  #直接进行序列化
print('序列化后的数据类型为:', type(dict_xu))
print('序列化后的数据为:', dict_xu)

未序列化前的数据类型为: <class 'dict'>
为序列化前的数据: {'name': 'zhangsan', 'address': '红星路', 'age': 33}
序列化后的数据类型为: <class 'str'>
序列化后的数据为: {"name": "zhangsan", "address": "\u7ea2\u661f\u8def", "age": 33}

在dumps函数中添加参数ensure_ascii=False,结果如下:

#coding: utf-8
import json

dict = {'name':'zhangsan', 'age':33, 'address':'红星路'}
print('未序列化前的数据类型为:', type(dict))
print('为序列化前的数据:', dict)
#对dict进行序列化的处理
dict_xu = json.dumps(dict,ensure_ascii=False)  #添加ensure_ascii=False进行序列化
print('序列化后的数据类型为:', type(dict_xu))
print('序列化后的数据为:', dict_xu)

未序列化前的数据类型为: <class 'dict'>
为序列化前的数据: {'address': '红星路', 'age': 33, 'name': 'zhangsan'}
序列化后的数据类型为: <class 'str'>
序列化后的数据为: {"address": "红星路", "age": 33, "name": "zhangsan"}

2、Json反序列化如下:

#coding: utf-8
import json
dict = {'name':'zhangsan', 'age':33, 'address':'红星路'}
print('未序列化前的数据类型为:', type(dict))
print('为序列化前的数据:', dict)
#对dict进行序列化的处理
dict_xu = json.dumps(dict,ensure_ascii=False)  #添加ensure_ascii=False进行序列化
print('序列化后的数据类型为:', type(dict_xu))
print('序列化后的数据为:', dict_xu)
#对dict_xu进行反序列化处理
dict_fan = json.loads(dict_xu)
print('反序列化后的数据类型为:', type(dict_fan))
print('反序列化后的数据为: ', dict_fan)

未序列化前的数据类型为: <class 'dict'>
为序列化前的数据: {'name': 'zhangsan', 'age': 33, 'address': '红星路'}
序列化后的数据类型为: <class 'str'>
序列化后的数据为: {"name": "zhangsan", "age": 33, "address": "红星路"}
反序列化后的数据类型为: <class 'dict'>
反序列化后的数据为:  {'name': 'zhangsan', 'age': 33, 'address': '红星路'}

在实际的工作中,序列化或者反序列化的可能是一个文件的形式,不可能像如上写的那样简单的,下来就来实现这部分,把文件内容进行序列化和反序列化,先来看序列化的代码,两步操作:1、先序列化 列表对象 ;2、步把序列化成的字符串写入文件:

#coding: utf-8
import json

list = ['Apple','Huawei','selenium','java','python']
#把list先序列化,写入到一个文件中
# 两步操作 1步先序列化 列表对象 2步把序列化成的字符串写入文件
json.dump(list, open('e:/test.txt','w'))  
r1=open('e:/test.txt','r')
print(r1.read())

["Apple", "Huawei", "selenium", "java", "python"]

反序列化,两步操作:1、先读取文件的字符串对象;2、然后反序列化成列表对象:

#coding: utf-8
import json

list = ['Apple','Huawei','selenium','java','python']
#把list先序列化,写入到一个文件中
# 两步操作 1步先序列化 列表对象 2步把序列化成的字符串写入文件
json.dump(list, open('e:/test.txt','w'))  
r1=open('e:/test.txt','r')
print(r1.read())
#------------------------------------------------------------
#两步操作:1、先读取文件的字符串对象;2、然后反序列化成列表对象
res=json.load(open('e:/test.txt','r'))
print (res)
print('数据类型:',type(res))

["Apple", "Huawei", "selenium", "java", "python"]
['Apple', 'Huawei', 'selenium', 'java', 'python']
数据类型: <class 'list'>

上述就是小编为大家分享的使用Python怎么实现Json的序列化与反序列化了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI