【问题标题】:Removing attributes from response of 'deep' JSON从“深度”JSON 的响应中删除属性
【发布时间】:2023-03-29 00:25:01
【问题描述】:

在我的 grails 控制器中,我像这样返回我的对象​​:

JSON.use("deep") {
respond details
}

我得到的 JSON 是:

[
    {
        "class": "com.evolving.resource.tn.TNDetails",
        "id": null,
        "ageToDate": null,
        "dnpk": "1290",
        "iccid": [
            {
                "class": "com.evolving.resource.iccid.ICCID",
                "id": 4209,
                "imsi": [
                    {
                        "class": "com.evolving.resource.imsi.IMSI",
                        "id": 13336,
                        "iccid": {
                            "_ref": "../..",
                            "class": "com.evolving.resource.iccid.ICCID"
                        },
                        "imsi": "234207300009975"
                    }
                ],
                "sim": "8944200000060007084",
                "tn": {
                    "_ref": "../..",
                    "class": "com.evolving.resource.tn.TNDetails"
                }
            }
        ],
        "permanentReservedFlag": null,
        "portInOldSP": "XX",
        "portOutNewSP": null,
        "reserveToDate": null,
        "tn": "447400002035"
    }
]

如何从响应 JSON 中删除一些不需要的标签,例如 classid_ref

我在 resources.groovy 文件中使用了JsonRenderer,但它没有用。

【问题讨论】:

标签: grails groovy


【解决方案1】:

实际上我们只需要在 bootstrap.groovy 文件中添加一个自定义 JSON 编组器,如下所示: 类引导程序 {

def init = { servletContext ->
    JSON.createNamedConfig("TNDetailsView", {
        JSON.registerObjectMarshaller(TNDetails) { TNDetails o ->
          return [
            tn : o.tn,
            sim : o.iccid.sim,
            imsi : o.iccid.imsi.imsi,
            ageToDate : o.ageToDate,
            permanentReservedFlag : o.permanentReservedFlag,
            portInOldSP : o.portInOldSP,
            portOutNewSP : o.portOutNewSP,
            reserveToDate : o.reserveToDate
          ]
        }
      })
}
def destroy = {
}
}

然后在控制器中做:

respond details 

它只会返回我们在上面提到的引导文件中的那些属性。

【讨论】:

    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多