Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Grails2.3.1中呈现来自控制器的JSON响应_Json_Grails - Fatal编程技术网

在Grails2.3.1中呈现来自控制器的JSON响应

在Grails2.3.1中呈现来自控制器的JSON响应,json,grails,Json,Grails,在我的Grails应用程序中,我有两个域:Person和County class Person { String firstName String lastName County county LocalDate dateOfBirth static hasMany = [episodes: Episode] } class County { String name // other stuff... } 当试图从我的

在我的Grails应用程序中,我有两个域:Person和County

class Person {
     String firstName
     String lastName
     County county
     LocalDate dateOfBirth
     static hasMany = [episodes: Episode]
 }
 class County {
     String name
     // other stuff...
 }
当试图从我的控制器呈现我的人员列表时,我只得到County{class:County,id:1}而不是County的名称。我想要与我的人相关的对象的属性

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond Person.list(params), model:[personInstanceCount: Person.count()]
}
我不想默认为deep转换器,然后我的belongsTo,还有很多关系似乎不起作用

grails.converters.json.default.deep = true
我尝试过CustomRenders,但失败了,Grails不关心我在那里做的任何更改

 personRenderer(JsonRenderer, Person) {
        excludes = ['class']
    }
    personsRenderer(JsonCollectionRenderer , Person){
        excludes = ['class']
    }
    countyRenderer(JsonRenderer, County) {
        excludes = ['class']
        includes = ['name']
    }
    countiesRenderer(JsonCollectionRenderer , County){
        excludes = ['class']
        includes = ['name']
    }
我尝试了
CustomMarshallerRegistrator
与上面相同的结果,完全没有发生任何事情,相同的结果。Se 8.1.6.2

那么,如何让Person对象包含相关的County而不仅仅是Class和ID属性呢


Im在Windows上使用Grails 2.3.1和jdk 1.7

如果您有兴趣将其作为JSON响应,可以尝试以下操作:

在引导中注册对象封送器

JSON.registerObjectMarshaller(Person) {
    def person = [:]
    ['firstName', 'lastName', 'country', 'dateOfBirth'].each { name ->
        person = it[name]
    }
    return person
}

JSON.registerObjectMarshaller(Country) {
    def country = [:]
    ['name'].each { name ->
        country = it[name]
    }
    return country
}
然后作为控制器的响应

render text: [personList:Person.list(params), personInstanceCount: Person.count()] as JSON, contentType: 'application/json'

如果您有兴趣将其作为JSON响应,可以尝试以下方法:

在引导中注册对象封送器

JSON.registerObjectMarshaller(Person) {
    def person = [:]
    ['firstName', 'lastName', 'country', 'dateOfBirth'].each { name ->
        person = it[name]
    }
    return person
}

JSON.registerObjectMarshaller(Country) {
    def country = [:]
    ['name'].each { name ->
        country = it[name]
    }
    return country
}
然后作为控制器的响应

render text: [personList:Person.list(params), personInstanceCount: Person.count()] as JSON, contentType: 'application/json'

我会接受你的回答,即使我觉得把这样的东西放在引导文件里很难闻。我最终遵循了这个指南。我会接受你的回答,即使我觉得把这样的东西放在引导文件里很难闻。我最终遵循了这个指南。