我有这个域类:
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
}
}
}
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以尝试将setRenderDomainClassRelations
上的JSON
设置为false,但我认为您真正需要的是使用构建器并进一步明确声明JSON结构:
render(builder:'json') {
success(true)
message('ok')
data {
office(rezult.office)
branch(rezult.branch)
// and so on
}
}
}