python实现⽐对两个json串的⽅法
  记录瞬间
前段时间为了解决⼀些实际问题,引出了要对json字符串进⾏⽐对的需求。
觉得有意义,作以简单记录。
# ⽐对数据
def compare_data(set_key, src_data, dst_data, noise_data, num):
if isinstance(src_data, dict) and isinstance(dst_data, dict):
"""若为dict格式"""
for key in dst_data:
if key not in src_data:
print("src不存在这个key")
noise_data[key] = "src不存在这个key"
for key in src_data:
if key in dst_data:
if src_data[key] != dst_data[key] and num == 1:
noise_data[key] = "容忍不等"
if src_data[key] != dst_data[key] and num == 2:
noise_data[key] = {}
noise_data[key]["primary"] = src_data[key]
noise_data[key]["candidate"] = dst_data[key]
"""递归"""
compare_data(key, src_data[key], dst_data[key], noise_data, num)
else:
noise_data[key] = ["dst不存在这个key"]
elif isinstance(src_data, list) and isinstance(dst_data, list):
"""若为list格式"""
if len(src_data) != len(dst_data) and len(set_key) != 0:
print("list len: '{}' != '{}'".format(len(src_data), len(dst_data)))
noise_data[set_key]["primary"] = str(src_data)
noise_data[set_key]["candidate"] = str(dst_data)
return
if len(src_data) == len(dst_data) and len(src_data) > 1:
for index in range(len(src_data)):
for src_list, dst_list in zip(sorted(src_data[index]), sorted(dst_data[index])):
"""递归"""
compare_data("", src_list, dst_list, noise_data, num)
else:
for src_list, dst_list in zip(sorted(src_data), sorted(dst_data)):
"""递归"""
compare_data("", src_list, dst_list, noise_data, num)
else:
if str(src_data) != str(dst_data):
print("src_data", src_data, "dst_data", dst_data)
return noise_data
代码实现了出⼊的src_data、dst_data两个字符串,noise_data作为记录两个字符串的差异进⾏返回。
set_key是对json串中存在key值对应的list列表时,⽐对了列表长度不等,我们认为不需要在对列表中的具体元素进⾏对⽐。num区分第⼀次对⽐和第⼆次对⽐时需要记录的选项。
# 分析获取的数据,并清空存储
def analyse_data():
noise_data1 = {}python json字符串转数组
mark_result = compare_data("", response["primary"], response["secondary"], noise_data1, 1)
noise_data2 = {}
comp_result = compare_data("", response["primary"], response["candidate"], noise_data2, 2)
td1 = "<tr><td style=\"background-color: #fcf8e3\">"
td2 = "</td><td style=\"width: 45%;padding: 8px;background-color: " \
"#dff0d8;line-height: 1.42857143;vertical-align: left;\"><pre style=\"white-space: pre-wrap!important;" \
"word-wrap: break-word!important;*white-space:normal!important;\">"
diff_td2 = "</td><td style=\"width: 45%;padding: 8px;background-color: " \
"#dff0d8;line-height: 1.42857143;vertical-align: left;\">"
diff_td3 = "</td><td style=\"width: 45%;padding: 8px;background-color: " \
"#dff0d8;line-height: 1.42857143;vertical-align: left;\">"
with open("./result.html", mode="a", encoding="UTF-8") as f:
print(td1 + "Response : ", td2 + json.dumps(response["primary"], indent=4, ensure_ascii=False) + "</pre>" +
td2 + json.dumps(response["candidate"], indent=4, ensure_ascii=False) + "</pre></td></tr>", file=f)
print("mark = ", mark_result)
print("comp = ", comp_result)
if (len(comp_result) - len(mark_result)) > 0:
diff_td = "<tr><td rowspan=\"" + str(len(comp_result) - len(mark_result)) + "\" style=\"background-color: #bf466a\">"
print("diff_td = ", diff_td)
with open("./result.html", mode="a", encoding="UTF-8") as f:
print(diff_td + "Diff Result ", file=f)
for key in comp_result:
if key not in mark_result:
try:
with open("./result.html", mode="a", encoding="UTF-8") as f:
print(diff_td2 + key + "->" + str(noise_data2[key]["primary"]) +
diff_td3 + str(noise_data2[key]["candidate"]) + "</td></tr><tr>", file=f)
# print(key + " = A:" + str(noise_data2[key]["primary"]) + "<->D:" + str(noise_data2[key]["candidate"])
            #    + " Maybe a new bug.", file=f)
print(key, "有可能是bug,要检查了。", str(noise_data2[key]["primary"]), str(noise_data2[key]["candidate"]))
except Exception as e:
with open("./result.html", mode="a", encoding="UTF-8") as f:
print(td1 + "Diff Result : ", td2 + str(noise_data2[key]["primary"]) + "</pre>" +
td2 + str(noise_data2[key]["candidate"]) + "</pre></td></tr>", file=f)
# print(key + " = A:" + str(noise_data2[key]["primary"]) + "<->D:" + str(noise_data2[key]["candidate"])
            #     + " Maybe a new bug.", file=f)
print(e)
noise_data1.clear()
noise_data2.clear()
response.clear()
这⾥的response作为全局的变量进⾏使⽤。我们在获取了噪⾳数据noise_data1和noise_data2时,对噪⾳数据进⾏⽐对。添加到result.html⽹页中,作为呈现的⽂本。
============================================

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。