在我的grails Controller 中,我正在像这样返回我的对象:
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中删除一些不需要的标签,例如
class
,id
和_ref
? 我在resources.groovy文件中使用了
JsonRenderer
,但没有用。
最佳答案
实际上,我们只需要在 bootstrap.groovy 文件中添加一个自定义JSON marshler,如下所示:
类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 = {
}
}
并在 Controller 中执行:
respond details
它只会返回引导文件中我们上面提到的那些属性。
关于grails - 从 'deep' JSON响应中删除属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29073538/