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

后端 未结 7 856
梦如初夏
梦如初夏 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:51

    import testproject.*
    import grails.converters.*  
    import grails.web.JSONBuilder
    
    def emp = new Employee()  
    emp.lastName = "Bar"  
    
    def excludedProperties = ['class', 'metaClass']
    
    def builder = new JSONBuilder.build {
      emp.properties.each {propName, propValue ->
    
      if (!(propName in excludedProperties)) {
        setProperty(propName, propValue)
      }
    }
    
    render(contentType: 'text/json', text: builder.toString())
    

提交回复
热议问题