从 'deep' JSON 的响应中删除属性

Removing attributes from response of 'deep' JSON

在我的 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,但没有用。

实际上我们只需要在 bootstrap.groovy 文件中添加自定义 JSON 编组器,如下所示: class BootStrap {

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 

它会 return 只有我们在上面 bootstrap 文件中提到的那些属性。