gpt4 book ai didi

json - Groovy比较两个具有未知节点名称和值的json

转载 作者:行者123 更新时间:2023-12-04 17:46:17 25 4
gpt4 key购买 nike

我有一个REST API进行测试,我必须比较两个json响应。您可以在下面找到文件的结构。要比较的两个文件应包含相同的元素,但顺序可能不同。不幸的是,名称,类型(简单,数组)和键数(根,nodeXYZ)也是未知的。

{"root": [{
"node1": "value1",
"node2": "value1",
"node3": [
{
"node311": "value311",
"node312": "value312"
},
{
"node321": "value321",
"node322": "value322"
}
],
"node4": [
{
"node411": "value411",
"node412": "value413",
"node413": [ {
"node4131": "value4131",
"node4132": "value4131"
}],
"node414": []
}
{
"node421": "value421",
"node422": "value422",
"node423": [ {
"node4231": "value4231",
"node4232": "value4231"
}],
"node424": []
}]
"node5": [
{"node51": "value51"},
{"node52": "value52"},
]
}]}

我发现了一些有用的信息
Groovy - compare two JSON objects (same structure) and return ArrayList containing differences
Getting node from Json Response
Groovy : how do i search json with key's value and find its children in groovy
但我无法将其组合为解决方案。
我认为解决方案可能看起来像这样:
take root
get root children names
check if child has children and get their names
do it to the lowest leve child

有了所有名称,比较起来应该很容易(我想)
不幸的是,我没有设法在根目录下获得 key

最佳答案

只需比较这些 map 即可:

def map1 = new JsonSlurper().parseText(document1)
def map2 = new JsonSlurper().parseText(document2)

assert map1 == map2

关于json - Groovy比较两个具有未知节点名称和值的json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33410830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com