如何以Json Grails-2.5.1的形式在映射中使用其他域对象或值呈现Json命名配置
我已经在Bootstrap.groovy中为我的域类“User”和“Role”注册了named config,因为我只希望在Grails2.5.1中呈现两个必填字段 BootStrap.groovy如何以Json Grails-2.5.1的形式在映射中使用其他域对象或值呈现Json命名配置,json,grails,gorm,Json,Grails,Gorm,我已经在Bootstrap.groovy中为我的域类“User”和“Role”注册了named config,因为我只希望在Grails2.5.1中呈现两个必填字段 BootStrap.groovy //.. def init = { servletContext -> JSON.createNamedConfig('thinUser') { it.registerObjectMarshaller( User ) { User user -&
//..
def init = { servletContext ->
JSON.createNamedConfig('thinUser') {
it.registerObjectMarshaller( User ) { User user ->
return [
id: user.id,
name: user.name
]
}
}
JSON.createNamedConfig('thinRole') {
it.registerObjectMarshaller( Role) { Role role->
return [
id: role.id,
name: role.name
]
}
}
}
//..
我想将用户域的对象以及其他一些对象呈现为Json
UserController.groovy
//..
def createUser(){
def customMap = [:]
def userJson = JSON.use('thinUser') {
User.list() as JSON
}
//println userJson is giving appropriate result
//[{"id":1,"name":"Peeyush"},{"id":2,"name":"Amit"}]
customMap.users = userJson
def roleJson = JSON.use('thinRole') {
Role.list() as JSON
}
//println roleJson is giving appropriate result
customMap.roles = roleJson
customMap.otherValue = 'someotherValue'
customMap.userId = 1
render customMap as JSON // Having issue here unable to get expected result
}
//..
我得到了这样的回应
{"users":{"class":"grails.converters.JSON","depth":0,"writer":{"class":"org.codehaus.groovy.grails.web.json.JSONWriter"}}, .....}
但预期的结果是
{"users":{{"id":1,"name":"Peeyush"},{"id":2,"name":"Amit"},{"id":3,"name":"Arpit"},{"id":4,"name":"Amit"}}, ....}
请帮助我呈现所需的json。
我使用了JSON.registerObjectMarshaller,但这将是一个问题,因为它将适用于整个应用程序,这将是一个问题
请建议我如何解决这个问题。grails的哪个版本?我使用的是grails 2.5.1那么,当您将customMap作为JSON时会遇到什么问题?grails的哪个版本?我使用的是grails 2.5.1那么,当您将customMap作为JSON时会遇到什么问题?