首页 > 解决方案 > Json序列化和反序列化

问题描述

我正在使用 python 中的 deepdiff 包比较两个嵌套字典。我想将它存储在一个文件中,但它给了我一个错误

'prettyordered set 不是 json 可序列化的'

我尝试使用它进行转换'to_dict',同样的错误。我也尝试使用 转换它'to_json',它解决了问题,但它在键和值中添加了反斜杠,而且在阅读时我无法阅读,它给了我一个错误:

'json.decoder.JSONDecodeError'

from deepdiff import DeepDiff
import json

variable1={'key':'key32','hello':'hello1'}
variable2={"key3":'key','hello':'hello2'}
result=DeepDiff(variable1,variable2)
result=result.to_json()
print(result)

filename='json_serializable'+'.txt'
objects_file = 'D:\\'+ filename
f = open(objects_file,'w')
f.write(json.dumps(result))

with open('D:\\Registryvalues\\'+filename) as json_file:
    variable1 = json.load(json_file)
print(variable1)

我想编写和读取我使用 deepdiff 获得的差异对象。有人可以帮助我吗?

标签: pythondeep-diff

解决方案


推荐阅读