Groovy JsonBuilder对象

时间:2018-07-18 13:47:28

标签: groovy jsonbuilder

我的JsonBuilder遇到了一些麻烦。我希望输出如下所示:

{
    "appointmentCheckResult": [
        {
            "itexxmCode": "98765432",
            " needAppointmentCheckFlag ": "Y"
        },
        {
            "itemCode": "98765433",
            "needAppointmentCheckFlag": "N"
        }
    ]
}

我得到的是:

{
    "appointmentCheckResult": {
        "xxx": [
            {
                "itemCode": "12345",
                "needAppointmentCheckFlag": "Y"
            },
            {
                "itemCode": "5678902",
                "needAppointmentCheckFlag": "Y"
            }
        ]
    }
}

代码如下所示:

import groovy.json.*

def json = new JsonBuilder()
def itemCode = ['12345', '5678902']
def needFlag = 'Y'
json.appointmentCheckResult{xxx([itemCode].transpose().collect {[itemCode:it[0], needAppointmentCheckFlag:needFlag]})}

println JsonOutput.prettyPrint(json.toString())

如何摆脱XXX和位于XXX前面的“ {”?

1 个答案:

答案 0 :(得分:0)

不知道您期望如何在输出中获得YN,或者将itexxmCode作为关键字...但是假设它们在预期输出中是错别字,则需要类似以下内容的

json {
    appointmentCheckResult(
        [itemCode].transpose().collect {
            [itemCode: it[0], needAppointmentCheckFlag: needFlag]
        }
    )
}