想要从休眠json响应中嵌套json格式 [英] want nested json format from hibernate json response

查看:90
本文介绍了想要从休眠json响应中嵌套json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下的json响应

I have json response like below

[{"Name":"kannur hub","Amount":1840.00},{"Name":"Calicut Hub","Amount":7000.00}]

我想要一个像这样的json,而不是上面的格式

i want a json like this instead of the above format

[{"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}]}]

因此,每当我从休眠的投影中检索子组时,结果删除总和值并返回单个值

So whenever i retrieve children grouping from hibernate projections,the result removes the sum value and return individual values

[{"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}]

休眠查询是,

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数据库

Iam using spring boot application and postgresql database with java 1.8 version

推荐答案

如果要获得所需的json结果,则应创建一个自定义DTO,请参见下面的示例。

You should create a custom DTO if you want to achieve the json results you want, please see example below.

class Child {
    String sub;
    Long amount;
}

class Dto {
   String name;
   Long totalAmount;
   List<Child> child;
}

然后编写Dto并添加必要的孩子。下面是示例,假设您已经获得了db的结果:

then compose your Dto and add necessary children. Below are the example, assumed that you already have the results from db:

如果您的rs返回为 List< Child> 然后您可以执行此操作。

if your rs return is List<Child> then you can do this..

Dto dto = new Dto();
dto.addAll(rs);
dto.setName("name");
dto.setTotalAmount(totalAmount);
return dto;

或者如果rs结果不是 List< Child> 您可以执行此操作...

or if rs result is not List<Child> you can do this...

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;

有效的结果JSON如下:

The valid resulting JSON is like below:

{
        "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
            }
        ]
    }

这篇关于想要从休眠json响应中嵌套json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆