Groovy:创建对象数组

时间:2019-03-04 20:35:04

标签: groovy

给出以下列表,我们如何提取路径和方法以创建json对象数组。

def List = [(proxy.pathsuffix MatchesPath "/{accountbillId}") and (request.verb = "POST"), (proxy.pathsuffix MatchesPath "/{accountbillId}") and (request.verb = "GET"), (proxy.pathsuffix MatchesPath "/") and (request.verb = "GET"), !(request.header.Accept JavaRegex "(text\/xml)(;.*)|(.*;)(text\/xml)(;.*)|text\/xml|(.*;)(text\/xml)|(application\/xml)(;.*)|(.*;)(application\/xml)(;.*)|application\/xml|(.*;)(application\/xml)|(application\/json)(;.*)|(.*;)(application\/json)(;.*)|application\/json|(.*;)(application\/json)"), (!(request.content = "")) and (!(request.content is null)) and !(request.header.Content-Type JavaRegex "(text\/xml)(;.*)|(.*;)(text\/xml)(;.*)|text\/xml|(.*;)(text\/xml)|(application\/xml)(;.*)|(.*;)(application\/xml)(;.*)|application\/xml|(.*;)(application\/xml)|(application\/json)(;.*)|(.*;)(application\/json)(;.*)|application\/json|(.*;)(application\/json)"), (request.verb == "OPTIONS") and (request.header.origin != null)]

需要构建最终的Json,如下所示:

{
    "paths": [
        {
            "path": "/{accountbillId}",
            "method": "POST"
        },
        {
            "path": "/{accountbillId}",
            "method": "GET"
        },
        {
            "path": "/"
            "method": "GET"
        }
    ]
}

到目前为止已经尝试过,但只能获得其中一个:

def sList = List
def pa = /(?<=proxy.pathsuffix MatchesPath ")([^"]+)/
def vb = /(?<=request.verb = ")([^"]+)/
def resultPA = sList.collect({
    def match = (it =~ pa)
    if (match.find()) {
        match.group()
    }
})
def pathparams = resultPA.findAll { it != null }
def resultVB = sList.collect({
    def match = (it =~ vb)
    if (match.find()) {
        match.group()
    }
})
def verbs = resultVB.findAll { it != null }
def jsonBody = [:]
def mm = verbs.collect {[method: it]}
def pp = pathparams.collect {[path: it]}
def pathx = [paths: pp]
    pathx = [paths: mm]
def finalBody = (JsonOutput.prettyPrint(JsonOutput.toJson(pathx)))
println finalBody

到目前为止,使用上述代码得到的结果:

{
    "paths": [
        {
            "method": "POST"
        },
        {
            "method": "GET"
        },
        {
            "method": "GET"
        }
    ]
}

{
    "paths": [
        {
            "path": "/{accountbillId}"
        },
        {
            "path": "/{accountbillId}"
        },
        {
            "path": "/"
        }
    ]
}

任何帮助或指导都将受到高度赞赏。谢谢

0 个答案:

没有答案