我有这个 Realm 类:
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
}
}
}
最佳答案
您可以尝试将setRenderDomainClassRelations
上的JSON
设置为false,但我想您真正需要的是使用构建器并进一步明确声明JSON结构:
render(builder:'json') {
success(true)
message('ok')
data {
office(rezult.office)
branch(rezult.branch)
// and so on
}
}
}
关于json - ils。域类。 Json字符串问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4299098/