【问题标题】:Groovy JsonBuilder objectsGroovy JsonBuilder 对象
【发布时间】:2018-07-18 13:47:27
【问题描述】:

我的 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前面的“{”?

【问题讨论】:

    标签: groovy jsonbuilder


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2016-07-11
      • 2012-06-13
      • 2014-09-06
      • 1970-01-01
      相关资源
      最近更新 更多