12

JSONコンバーターのクラスフィールドを削除する方法はありますか?

例:

import testproject.*
import grails.converters.*  
emp = new Employee()  
emp.lastName = "Bar"  
emp as JSON  

文字列として

{"class":"testproject.Employee","id":null,"lastName":"Bar"}

私は好む

{"id":null,"lastName":"Bar"}

クラスフィールドを削除するために、最後にもう1行のコードを追加する方法はありますか?

4

7 に答える 7

12

これを行う方法はまだ1つあります。ドメインクラスに次のコードを追加しました。

static {
    grails.converters.JSON.registerObjectMarshaller(Employee) {
    return it.properties.findAll {k,v -> k != 'class'}
    }
}

しかし、Groovy @ToStringクラスアノテーションを使用した場合、パラメータを除外するために'class'も追加する必要がある場合は、次のようになります。

@ToString(includeNames = true, includeFields = true, excludes = "metaClass,class")
于 2012-09-24T05:12:39.530 に答える
7

これを行うための私の好ましい方法:

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)
}

これにより、Book.getAllBoks()からすべてのオブジェクトが返されますが、collectメソッドはALLを指定した形式に変更します。

于 2012-10-12T08:34:18.553 に答える
3

1つの代替方法は、ビルダーを使用しないことです。

def myAction = {
    def emp = new Employee()
    emp.lastName = 'Bar'

    render(contentType: 'text/json') {
        id = emp.id
        lastName = emp.lastName
    }
}

従業員が変更された場合はレンダリングを変更する必要があるため、これは少し直交性が低くなります。一方、レンダリングされる内容をより細かく制御できます。

于 2011-06-27T16:09:36.590 に答える
1
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())
于 2011-06-28T08:01:40.563 に答える
1

@wwarlockの答えは部分的に正しいです。私はregisterObjectMarshallerをBootstrapに配置する必要があります。それは機能します。

于 2013-09-06T01:38:48.037 に答える
1
def a = Employee.list()

String[] excludedProperties=['class', 'metaClass']
render(contentType: "text/json") {
    employees = array {
        a.each {
            employee it.properties.findAll { k,v -> !(k in excludedProperties) }
        }
    }
}

これは私のために働きます。除外するプロパティを簡単に渡すことができます。またはそれを好転させます:

def a = Employee.list()

String[] includedProperties=['id', 'lastName']
render(contentType: "text/json") {
    employees = array {
        a.each {
            employee it.properties.findAll { k,v -> (k in includedProperties) }
        }
    }
}

注意:これは単純なオブジェクト専用です。「キーの置き忘れ:キーの予期されたモードですが、オブジェクトでした」と表示された場合、この解決策は適していません。:)

HP

于 2014-07-30T13:01:06.570 に答える
1

grails.converters.JSONで提供されているsetExcludesメソッドを使用して、除外するフィールド(クラス名を含む)をカスタマイズできます。

def converter = emp  as JSON
converter.setExcludes(Employee.class, ["class",""])

そして、あなたはあなたの要件に従ってそれを同じように使うことができます、

println converter.toString()
converter.render(new java.io.FileWriter("/path/to/my/file.xml"))
converter.render(response)
于 2016-09-30T09:12:30.283 に答える