Grails - grails.converters.JSON - removing the class name

后端 未结 7 852
梦如初夏
梦如初夏 2020-12-16 00:14

Is there a way to remove the class field in a JSON converter?

Example:

import testproject.*
import grails.converters.*  
emp = new Employee()  
emp         


        
7条回答
  •  半阙折子戏
    2020-12-16 00:42

    My preferred way of doing this:

    def getAllBooks() {
        def result = Book.getAllBooks().collect {
            [
                title: it.title,
                author: it.author.firstname + " " + it.author.lastname,
                pages: it.pageCount,
            ]
        }
        render(contentType: 'text/json', text: result as JSON)
    }
    

    This will return all the objects from Book.getAllBoks() but the collect method will change ALL into the format you specify.

提交回复
热议问题