3

I am trying to add a dynamic property to each instance of a class and then convert a list of such objects to json. Refer to the example below

class Author{
    String name
    static hasMany=[books:Book]
}

class Book{
    String title
    static belongsTo=[author:Author]
}

my controller

class AuthorController{
    def listAuthors={
        def authorsList=Author.list()
        def details=['address':'xyz', phone:'123']
        authorList.each{
            it.metaClass.getDetails={->details}
        }

        render authorsList as JSON
    }
}

now i want this dynamically added "details" property to also get rendered as JSON. please help

4

1 回答 1

1

如果不是创建一个设置器,而是添加一个属性,它是否有效:

it.metaClass.details = details

如果没有,您的最佳路径可能是从您的authorsList(将详细信息键/值对添加到每个地图)创建一个地图列表,然后将其序列化...

于 2012-04-19T08:04:48.140 回答