2010-11-28 88 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 
    } 
    } 
}