Python – 使用jsonpickle库对Python类对象进行json序列化和json反序列化操作
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:Python – 使用jsonpickle库对Python类对象进行json序列化和json反序列化操作
原文链接:https://www.stubbornhuang.com/1469/
发布于:2021年07月30日 15:29:49
修改于:2021年07月30日 15:29:49

1 Python类对象的json序列化和json反序列化
python类对象json序列化的定义:将类对象转换为可通过网络传输或可以存储到本地磁盘的Json数据格式的过程称为序列化;
python类对象json反序列化的定义:将Json转化为python类对象的过程称为反序列化;
2 jsonpickle库介绍
官方地址:http://jsonpickle.github.io/,关于该库的更多用法请参考官方文档。
2.1 jsonpickle安装
- pip
pip install jsonpickle
- conda
conda install jsonpickle
2.2 jsonpickle的使用
3 使用jsonpickle库对Python类对象序列化和反序列化
3.1 序列化
代码:
import jsonpickle
class RequestArgs:
def __init__(self,name,video_path):
self.name = name
self.video_path = video_path
class RequestApi:
def __init__(self, funcode, token, reqeuestargs_object):
self.funcode = funcode
self.token = token
self.args = reqeuestargs_object
if __name__ == '__main__':
requestApi = RequestApi('101101','2ec45770-b897-4cd0-8ae1-e65ab065a661',RequestArgs('liming','liming.mp4'))
john_string = jsonpickle.encode(requestApi,unpicklable=False)
print(john_string)
序列化结果:
{
"funcode": "101101",
"token": "2ec45770-b897-4cd0-8ae1-e65ab065a661",
"args": {
"name": "liming",
"video_path": "liming.mp4"
}
}
3.2 反序列化
import jsonpickle
class RequestArgs:
def __init__(self,name,video_path):
self.name = name
self.video_path = video_path
class RequestApi:
def __init__(self, funcode, token, reqeuestargs_object):
self.funcode = funcode
self.token = token
self.args = reqeuestargs_object
if __name__ == '__main__':
json_str = '{"funcode": "101101", "token": "2ec45770-b897-4cd0-8ae1-e65ab065a661", "args": {"name": "liming", "video_path": "liming.mp4"}}'
requestApi_dict = jsonpickle.decode(json_str)
requestApi = RequestApi(requestApi_dict['funcode'],requestApi_dict['token'],RequestArgs(requestApi_dict['args']['name'],requestApi_dict['args']['video_path']))
print(requestApi.funcode)
print(requestApi.token)
print(requestApi.args.name)
print(requestApi.args.video_path)
结果:
101101
2ec45770-b897-4cd0-8ae1-e65ab065a661
liming
liming.mp4
当前分类随机文章推荐
- 解决Python爬虫在爬资源过程中使用urlretrieve函数下载文件不完全且避免下载时长过长陷入死循环,并在下载文件的过程中显示下载进度 阅读3170次,点赞0次
- Python - 使用websockets库构建websocket服务器 阅读1001次,点赞0次
- Python - 运行YOLOv5出现AttributeError: module 'torchvision' has no attribute 'ops' 阅读404次,点赞0次
- Python - 配置Yolov5出现ImportError: cannot import name 'PILLOW_VERSION' from 'PIL'错误 阅读406次,点赞0次
- Python - 使用Opencv-Python库获取本机摄像头视频并保存为视频文件 阅读1764次,点赞0次
- Python - Linux/Centos/Ubuntu查看CUDA/cuDNN版本号 阅读1925次,点赞0次
- Python - 使用Python+websockets时报错:AttributeError: module 'websockets' has no attribute 'serve' 阅读981次,点赞0次
- Python - 解决opencv-python使用cv2.imread()读取中文路径图片失败的问题 阅读42次,点赞0次
- Python - BeautifulSoup的find()和findAll() 阅读2179次,点赞0次
- Python - 列表list遍历方法总结 阅读25次,点赞0次
全站随机文章推荐
- WordPress - 插件OSS Upload与WP Editor.md/WP Githuber MD插件冲突,导致katex公式不正确显示的问题 阅读2490次,点赞0次
- 资源分享 - 深度学习 花书 AI圣经(Deep Learning) 中文PDF下载 阅读3137次,点赞1次
- 资源分享 - GPU Gems 1 - Programming Techniques, Tips and Tricks for Real-Time Graphics英文高清PDF下载 阅读2383次,点赞0次
- 工具软件推荐 - 几个阅读英文文献的免费中文翻译科研利器推荐 阅读555次,点赞1次
- 资源分享 - Artificial Intelligence - A Modern Approach , First Edition 英文高清PDF下载 阅读770次,点赞0次
- 资源分享 - Real-Time Rendering, Second Edition 英文高清PDF下载 阅读1160次,点赞0次
- 资源分享 - 白话大数据与机器学习(高扬著)PDF下载 阅读1869次,点赞0次
- 资源分享 - Jim Blinn's Corner - Notation, Notation, Notation 英文高清PDF下载 阅读1241次,点赞0次
- TensorRT - Windows下TensorRT下载与配置 阅读974次,点赞0次
- 资源分享 - Graphics Programming Methods 英文高清PDF下载 阅读1179次,点赞0次
评论
147