满足条件时,使用Groovy从JSON中拉出嵌套的Maps / EntrySet

时间:2015-01-22 18:42:03

标签: json groovy jsonslurper

在代码领域遇到大麻烦,因此我需要帮助!

使用Groovy和JsonSlurper我正在处理下面表单的JSON。一个"类型"我正在寻找外部包含元素(在我的情况下,这一切都应该是地图)。 key设置为某个值。例如,如果类型是" Type5",那么我需要返回三个地图:' body'包含外部Type5,' body'的地图包含INNER Type5的映射,以及靠近底部的Type5映射(每个的EntrySet也可以正常工作)。 Type3和Type4表现出相同的行为!

根据请求编辑以拥有有效的Json

我通过Groovy的JsonSlurper运行它,所以它应该是有效的。

{
"type": "Run1",
"body": [
{
  "type": "Type1",
  "expression": {
    "type": "Type2",
    "expressions": [
      {
        "type": "Type3",
        "body": {
          "type": "Type4",
          "id": null,
          "params": [
            {
              "type": "Identifier",
              "name": "a"
            },
            {
              "type": "Identifier",
              "name": "b"
            },
            {
              "type": "Identifier",
              "name": "c"
            }
          ],
          "body": {
            "type": "Type5",
            "body": [
              {
                "type": "Type6",
                "id": {
                  "type": "Identifier",
                  "name": "d"
                },
                "params": [
                  {
                    "type": "Identifier",
                    "name": "a"
                  }
                ],
                "body": {
                  "type": "Type5",
                  "body": [
                    {
                      "type": "Type7",
                      "contents": {
                        "type": "Type8",
                        "leftcontents": {
                          "type": "Literal",
                          "value": "specific name",
                        },
                        "rightcontents": {
                          "type": "Type3",
                          "body": {
                            "type": "Type4",
                            "object": {
                              "type": "Identifier",
                              "name": "o"
                            },
                            "property": {
                              "type": "Identifier",
                              "name": "f"
                            }
                          },
                          "contents": [
                            {
                              "type": "Identifier",
                              "name": "a"
                            }
                          ]
                        }
                      }
                    }
                  ]
                },
              },
              {
                "type": "Type6",
                "id": {
                  "type": "Identifier",
                  "name": "e"
                },
                "params": [
                  {
                    "type": "Identifier",
                    "name": "a"
                  }
                ],
                "defaults": [],
                "body": {
                  "type": "Type5",
                  "body": [
                    {
                      "type": "Type7",
                      "contents": {
                        "type": "Type8",
                        "leftcontents": {
                          "type": "Literal",
                          "value": "string",
                        },
                        "rightcontents": {
                          "type": "Type9",
                          "argument": {
                            "type": "Identifier",
                            "name": "a"
                          },
                          "prefix": true
                        }
                      }
                    }
                  ]
                },
              }
            ]
          }
        }
      }
    ]
  }
}
]
}

我只是这样做:

FileInputStream fis = new FileInputStream('myInput.json')
def jsonData = (new JsonSlurper()).parse(fis)

虽然我可以轻松访问结构的各个部分,但是为了获得具有任意嵌套级别的所有" Type5" s超出了我的范围。任何人都能在这上面闪耀一些光彩吗?

1 个答案:

答案 0 :(得分:10)

基本上,您将收集所有地图并递归集合和所有地图值。 E.g:

def json='{"type":"Run1","body":[{"type":"Type1","expression":{"type":"Type2","expressions":[{"type":"Type3","body":{"type":"Type4","id":null,"params":[{"type":"Identifier","name":"a"},{"type":"Identifier","name":"b"},{"type":"Identifier","name":"c"}],"body":{"type":"Type5","body":[{"type":"Type6","id":{"type":"Identifier","name":"d"},"params":[{"type":"Identifier","name":"a"}],"body":{"type":"Type5","body":[{"type":"Type7","contents":{"type":"Type8","leftcontents":{"type":"Literal","value":"specificname",},"rightcontents":{"type":"Type3","body":{"type":"Type4","object":{"type":"Identifier","name":"o"},"property":{"type":"Identifier","name":"f"}},"contents":[{"type":"Identifier","name":"a"}]}}}]},},{"type":"Type6","id":{"type":"Identifier","name":"e"},"params":[{"type":"Identifier","name":"a"}],"defaults":[],"body":{"type":"Type5","body":[{"type":"Type7","contents":{"type":"Type8","leftcontents":{"type":"Literal","value":"string",},"rightcontents":{"type":"Type9","argument":{"type":"Identifier","name":"a"},"prefix":true}}}]},}]}}}]}}]}'
def slrp = new groovy.json.JsonSlurper().parseText(json)

def collectMaps(e) {
    e.with{
        if (it instanceof Map) {
            [it] + it.values().collect{ collectMaps(it) }
        } else if (it instanceof Collection) {
            it.collect{ collectMaps(it) }
        } else {
            []
        }
    }.flatten()
}

assert collectMaps(slrp).findAll{ it.type=='Type5' }.size()==3
assert collectMaps(slrp).findAll{ it.type=='Type3' }.size()==2