Json序列化和反序列化

2024-06-08 17:15:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

'prettyordered set is not json serializable'

我试着用'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得到的difference对象。有人能帮我吗


Tags: tokeyimportjsonhello错误resultfilename