2010-11-28 121 views
0

我有這個領域類:Grails。領域類。 JSON字符串問題

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

回答

1

你可以嘗試和設置setRenderDomainClassRelationsJSON爲假,但我想你是真的需要使用構建器並進一步明確聲明JSON結構:

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