Java 需要hibernate json响应中的嵌套json格式吗
我有如下的json响应Java 需要hibernate json响应中的嵌套json格式吗,java,json,postgresql,spring-boot,Java,Json,Postgresql,Spring Boot,我有如下的json响应 [{"Name":"kannur hub","Amount":1840.00},{"Name":"Calicut Hub","Amount":7000.00}] 我想要这样的json,而不是上面的格式 [{"name":"kannur hub","TotalAmount":1840,"child":[{"sub":"Sale Of Products @ 12 % Tax","amount":345,"sub":"sos","amount":1020,"sub":"Bos
[{"Name":"kannur hub","Amount":1840.00},{"Name":"Calicut Hub","Amount":7000.00}]
我想要这样的json,而不是上面的格式
[{"name":"kannur hub","TotalAmount":1840,"child":[{"sub":"Sale Of Products @ 12 % Tax","amount":345,"sub":"sos","amount":1020,"sub":"Boss","amount":475}]},{"name":"Calicut Hub","TotalAmount":7000,"child":[{sub":"cop","amount":3500,"sub":"SALES ACCOUNT","amount":3500}]}]
因此,每当我从hibernate投影中检索子分组时,结果将删除总和值并返回单个值
[{"sub":"Boss","Name":"kannur hub","Amount":475.00},{"sub":"sos","Name":"kannur hub","Amount":1020.00},{"sub":"cop","Name":"Calicut Hub","Amount":3500.00},{"sub":"SALES ACCOUNT","Name":"Calicut Hub","Amount":3500.00},{"sub":"Sale Of Products @ 12 % Tax","Name":"kannur hub","Amount":345.00}]
hibernate查询是
ProjectionList proj = Projections.projectionList();
proj.add(Projections.groupProperty("offId.officeProfileName").as("Name"));
proj.add(Projections.groupProperty("accId.accHeadName").as("sub"));
proj.add(Projections.sum("accountsDataValue").as("Amount"));
crit.setProjection(proj);
Iam使用spring boot应用程序和具有java 1.8版本的postgresql数据库如果要获得想要的json结果,请参见下面的示例
class Child {
String sub;
Long amount;
}
class Dto {
String name;
Long totalAmount;
List<Child> child;
}
或者,如果rs结果不是列表
,您可以这样做
Dto dto = new Dto();
//assumed rs contains the db child results.
for(int i=0; i<rs.length; i++) {
Child child = new Child(rs.get("sub"), rs.get("amount"))
dto.getChild().add(child)
}
dto.setName("name");
dto.setTotalAmount(totalAmount);
return dto;
特别是在创建DTO类之后,您是否可以更具体一些
Dto dto = new Dto();
//assumed rs contains the db child results.
for(int i=0; i<rs.length; i++) {
Child child = new Child(rs.get("sub"), rs.get("amount"))
dto.getChild().add(child)
}
dto.setName("name");
dto.setTotalAmount(totalAmount);
return dto;
{
"name":"kannur hub",
"TotalAmount":1840,
"child":[
{
"sub":"Sale Of Products @ 12 % Tax",
"amount":345,
},
{
"sub":"sos",
"amount":1020,
},
{
"sub":"Boss",
"amount":475
}
]
},
{
"name":"Calicut Hub",
"TotalAmount":7000,
"child":[
{
"sub":"cop",
"amount":3500
},
{
"sub":"SALES ACCOUNT",
"amount":3500
}
]
}