如何将复杂域对象解析为Grails中的JSON对象?

时间:2014-07-17 09:24:31

标签: json grails

好的,首先,我对grails非常新。我一直坚持将域对象转换为JSON。

我的域类如下所示

class MoneyTransfer {

    Account fromAccount
    Date sourceTransactionDate
    TransactionStatus sourceTransactionStatus
    String sourceTransactionMessage

    Account toAccount
    Date destinationTransactionDate
    TransactionStatus destinationTransactionStatus
    String destinationTransactionMessage

    double amount

    String note

    Status status

    PianoUser creator

    String errorMessage

    // predefined grails date create & modified & version
    Date dateCreated

    Date lastUpdated

    String uniqueId

}

如何将这样的域类对象转换为JSON对象?

我尝试使用grails.converters.JSONgrails.converters.deep.JSON,如下所示

class MyTransferController{
     def xyz(){
        MoneyTransfer monetTransferInstance = getMoneyTransferInstance();
        def moneyTransferJson = fundTrasnferInstance as JSON //doesnot work 
    }
}

如何将域类的对象转换为JSON对象?任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:1)

试试这个

import grails.converters.JSON

class MyTransferController {
     def xyz() {
        MoneyTransfer moneyTransferInstance = getMoneyTransferInstance()
        render moneyTransferInstance as JSON 
    }
}