当条件满足时,用 Groovy 从 JSON 中取出嵌套的 Maps/EntrySets
Pull out nested Maps/EntrySets from JSON with Groovy when criteria met
在代码领域遇到了大麻烦,因此我需要帮助!
使用 Groovy 和 JsonSlurper 我正在处理下面表格的 JSON。当 "type" 键设置为某个值时,我正在寻找外部包含元素(在我的例子中,这一切都应该是地图)。例如,如果类型是 "Type5",那么我需要取回三个 Map:包含外部 Type5 的 'body' Map,包含 INNER Type5 的 'body' Map,以及底部附近的 Type5 Map(每个 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 超出了我的范围。任何人都可以在这方面大放异彩吗?
基本上,您会收集所有地图并递归集合和所有地图值。例如:
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
在代码领域遇到了大麻烦,因此我需要帮助!
使用 Groovy 和 JsonSlurper 我正在处理下面表格的 JSON。当 "type" 键设置为某个值时,我正在寻找外部包含元素(在我的例子中,这一切都应该是地图)。例如,如果类型是 "Type5",那么我需要取回三个 Map:包含外部 Type5 的 'body' Map,包含 INNER Type5 的 'body' Map,以及底部附近的 Type5 Map(每个 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 超出了我的范围。任何人都可以在这方面大放异彩吗?
基本上,您会收集所有地图并递归集合和所有地图值。例如:
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