如果已经回答了这个问题,请联系我回答.
我的问题是我想获取无序的多层json的差异.
x=json.loads('''[{"y":2,"x":1},{"x":3,"y":4}]''')
y=json.loads('''[{"x":1,"y":2},{"x":3,"y":4}]''')
z=json.loads('''[{"x":3,"y":4},{"x":1,"y":2}]''')
import json_tools as jt
import json_delta as jd
print jt.diff(y,z)
print jd.diff(y,z)
print y==z
print x==y
输出是
[{'prev': 2, 'value': 4, 'replace': u'/0/y'}, {'prev': 1, 'value': 3, 'replace': u'/0/x'}, {'prev': 4, 'value': 2, 'replace': u'/1/y'}, {'prev': 3, 'value': 1, 'replace': u'/1/x'}]
[[[2], {u'y': 2, u'x': 1}], [[0]]]
False
True
我的问题是如何使y和z相等,或者是否存在实际差异,具体取决于JSON的非顺序.
一种无序的字典列表,但是我正在寻找一种级别级别的东西,即列表/字典的列表/字典…
解决方法:
看看这个python库jsondiff,它将帮助您识别差异的
import json
import jsondiff
json1 = json.loads(
'{"isDynamic": false, "name": "", "value": "SID:<sid>", "description": "instance","argsOrder": 1,"isMultiSelect": false}')
json2 = json.loads(
'{ "name": "", "value": "SID:<sid>","isDynamic": false, "description": "instance","argsOrder": 1,"isMultiSelect": false}')
res = jsondiff.diff(json1, json2)
if res:
print("Diff found")
else:
print("Same")