0

这是我试图解析的 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”。这是我试图用它来实现的一个时髦的脚本,

    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] 但我目前为空。仅从“ListContent”读取“id”并将其写入数组的正确常规应该是什么?任何帮助将非常感激。提前致谢。

4

2 回答 2

1
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 }

正如您已经List从 ( context.expand( '${TestStep#Response#$.data.List}' )) 中获得的那样,您可以简单地执行以下操作:

outputResp.collect { it.ListContent.id }

上面返回一个ArrayList.

于 2015-09-29T17:39:08.817 回答
1

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

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]
于 2015-09-29T17:45:01.347 回答