gpt4 book ai didi

json - 当条件满足时,使用 Groovy 从 JSON 中提取嵌套的 Maps/EntrySets

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

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

使用 Groovy 和 JsonSlurper 我正在处理以下形式的 JSON。当“类型”键设置为某个值时,我正在寻找外部包含元素(在我的情况下,这一切都应该是 map )。例如,如果类型是“Type5”,那么我需要取回三个 Maps:包含外部 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”,这超出了我的能力。任何人都可以在这方面发光吗?

最佳答案

基本上,您将收集所有 map 并在集合和所有 map 值上进行递归。例如。:

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

关于json - 当条件满足时,使用 Groovy 从 JSON 中提取嵌套的 Maps/EntrySets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28096354/

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