json使用groovy收集值进行解析

时间:2017-02-09 14:45:21

标签: json parsing groovy

我有以下json文本,我的目标是查找是否有任何“直接”字段的值为'false'为meta.name =“name1”

我可以解析并获取“name1”的列表,但不知道如何更进一步..比如收集字段“direct”的值!= true并检查集合是否超过0。

def list = new groovy.json.JsonSlurper().parseText(text )
println list.record.land.find{ it.meta.name == 'name1' } 



{
  "version": 1,
  "record": {
    "land": [
      {
        "meta": {
          "name": "name1",
          "type": "person"
        },
        "delete": {
          "result": "allow",
          "factor": {
            "direct": true,
            "rules": [
              {
                "rule1": "1002",
                "uri": "/deeds/567576-ffgt-1002"
              },
              {
                "rule1": "5012",
                "uri": "/deeds/567576-dfge-5012"
              }
            ]
          }
        },
        "transfer": {
          "result": "allow",
          "factor": {
            "direct": true,
            "rules": [
              {
                "rule1": "1002",
                "uri": "/deeds/567576-ffgt-1002"
              },
              {
                "rule1": "5012",
                "uri": "/deeds/567576-dfge-5012"
              }
            ]
          }
        },
         "move": {
          "result": "allow",
          "factor": {
            "direct": false,
            "rules": [
              {
                "rule1": "1002",
                "uri": "/deeds/567576-ffgt-1002"
              },
              {
                "rule1": "5012",
                "uri": "/deeds/567576-dfge-5012"
              }
            ]
          }
        }

  }
  ]
}
}

1 个答案:

答案 0 :(得分:1)

在第一步中,您可以过滤json以查找具有特定名称的所有对象:

def filtered = list.record.land.findAll{ it.meta.name == 'name1'}

然后你可以这样:

def directIsFalse = ['delete', 'transfer', 'move'].any{
     filtered[it].factor.direct[0] == false;
}

如果在至少一个部分("删除","转移"或"移动")字段直接将等于{{1 }}