簡體   English   中英

Grails的。 域類。 Json字符串問題

[英]Grails. Domain class. Json string problem

我有這個領域類:

package test

class Credit {


    String office;
    String branch;
    String name;
    Integer code;
    Integer number;
    Integer term;
    Integer amount;
    Integer rate;


    static hasMany = [  debts : Debt, 
                fronts : Front,
                securities : Security,
                lawyers : Lawyer,
                guarantes : Guarante]


    static constraints = {
    }
}

我需要創建一個字符串JSON,它只包含有關這些字段的信息:

String office;
        String branch;
        String name;
        Integer code;
        Integer number;
        Integer term;
        Integer amount;
        Integer rate;

我嘗試:

rezult = Credit.list(fetch:[debts:"lazy", fronts: 'lazy', securities: "lazy", lawyers:"lazy", quarantes:"lazy"])
render new JSON(success: true, message: 'ok', data:rezult);

但是在JSON字符串中我擁有所有數據:(債務,前額,證券...也在字符串內。但是我不需要此數據。

如何避免使用它們?

回答:

render(contentType:"text/json") {
    success=true
    message='ok'
    totalCount=Credit.count()
    data = array {
        for(d in results) {
            data    office:d.office,
                    branch:d.branch, 
                    name: d.name,
                    code:d.code, 
                    number:d.number,
                    term:d.term,
                    amount:d.amount,
                    rate:d.rate
        }
    }   
}

您將必須使用json構建器來解決此問題

來自博客的樣本

Grails JSON Builder文檔

您可以嘗試將JSON上的setRenderDomainClassRelations設置為false,但我想您真正需要的是使用構建器並進一步明確聲明JSON結構:

render(builder:'json') {
  success(true)
  message('ok')
  data {
    office(rezult.office)
    branch(rezult.branch)
    // and so on
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM