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

时间:2015-09-29 17:35:04

标签: json groovy soapui jsonslurper

这是我试图解析的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只能阅读" id"来自" ListContent"并将其写入数组? 任何帮助将非常感激。 提前谢谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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]