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

标签 json grails groovy jsonslurper

我有一个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/

相关文章:

javascript - Groovy Grails 发送/接收变量

java - DHXScheduler 保存/获取自定义灯箱字段

hibernate - 如何在Grails条件查询中引用 “this”对象

java - 如何创建Scene2d标签?

Grails 3 : External Logback. 常规文件

grails - 使用共享缓存和 session 部署多个 Grails 实例?

eclipse - 在 Eclipse 中开发 jenkins Groovy 脚本

testing - 似乎 Groovy 在这两种情况下的行为不同?

json - 如何在 MonetDB 中使用 JSON

json - 为什么 JSON.parse ("string") 失败