如何解析 Json 响应并截断子节点

How to parse Json response and truncate child nodes

这是我要解析的 JSON 响应:

{
"data": {
    "Content": {
        "id": 26,
        "name": "Dashboard1"
    },
    "List": [
        {
            "ListContent": {
                "id": 178,
                "name": "Card-144"
            },
            "cards": [
                {
                    "id": 1780,
                    "configuration": {
                        "id": 7178,
                        "name": "Emp"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 179,
                "name": "Card-14"
            },
            "cards": [
                {
                    "id": 1798,
                    "configuration": {
                        "id": 1789,
                        "name": "RandomColumns"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 180,
                "name": "Card-1"
            },
            "cards": [
                {
                    "id": 18080,
                    "configuration": {
                        "id": 1080,
                        "allow": true
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 181,
                "name": "Card-14"
            },
            "cards": [
                {
                    "id": 18081,
                    "configuration": {
                        "id": 1881,
                        "name": "Functions"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 182,
                "name": "Card-1443"
            },
            "cards": [
                {
                    "id": 1782,
                    "configuration": {
                        "id": 1802,
                        "name": "Emp-O"
                    }
                }
            ]
        }
    ]
}

}

从 Json 中,我需要提取 "ListContent" 节点下的 "id" 并将其存储在数组中。此外,将需要忽略子节点下的 "id"s。 这是一个 groovy 脚本,我试图用它来实现,

    def CList = ""
    import groovy.json.JsonSlurper
    def jsonRespData = context.expand( '${TestStep#Response#$.data.List}' ) 
    def outputResp = new JsonSlurper().parseText(jsonRespData)


    outputResp.id.each()
    {log.info( ":"+ it) 
    CList=CList.concat(it.toString()).concat(',')} 


      log.info (CList)

所以,我期待的数组是 CList [178,179,180,181,182] 但我现在越来越空了。 什么应该是正确的 groovy 只从 "ListContent" 读取 "id" 并将其写入数组? 任何帮助将非常感激。 提前致谢。

def str = '''
{
"data": {
    "Content": {
        "id": 26,
        "name": "Dashboard1"
    },
    "List": [
        {
            "ListContent": {
                "id": 178,
                "name": "Card-144"
            },
            "cards": [
                {
                    "id": 1780,
                    "configuration": {
                        "id": 7178,
                        "name": "Emp"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 179,
                "name": "Card-14"
            },
            "cards": [
                {
                    "id": 1798,
                    "configuration": {
                        "id": 1789,
                        "name": "RandomColumns"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 180,
                "name": "Card-1"
            },
            "cards": [
                {
                    "id": 18080,
                    "configuration": {
                        "id": 1080,
                        "allow": true
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 181,
                "name": "Card-14"
            },
            "cards": [
                {
                    "id": 18081,
                    "configuration": {
                        "id": 1881,
                        "name": "Functions"
                    }
                }
            ]
        },
        {
            "ListContent": {
                "id": 182,
                "name": "Card-1443"
            },
            "cards": [
                {
                    "id": 1782,
                    "configuration": {
                        "id": 1802,
                        "name": "Emp-O"
                    }
                }
            ]
        }
    ]
}
}
'''

def outputResp = new groovy.json.JsonSlurper().parseText(str)
outputResp.data.List.collect { it.ListContent.id }

由于您已经从 (context.expand( '${TestStep#Response#$.data.List}' )) 获得了 List,您可以简单地执行以下操作:

outputResp.collect { it.ListContent.id }

在 returns 之上 ArrayList

您可以像这样使用(隐式)展开运算符:

def json = new groovy.json.JsonSlurper().parse('/tmp/x.json' as File)

// 
def i = json.data.List.ListContent.id
assert i == [178, 179, 180, 181, 182]

// with explicit spread operator
def e = json.data.List*.ListContent*.id
assert e == [178, 179, 180, 181, 182]