简体   繁体   English

如何在 java spring 引导中对 json 数组的两个值求和?

[英]How to sum value of two values of a json array in java spring boot?

My Api response(for the url http://localhost:8080/findTotSum/12-01-2020/12-02-2020) in JSON format is this My Api response(for the url http://localhost:8080/findTotSum/12-01-2020/12-02-2020) in JSON format is this

[ { "id": 121982, "expdate": null, "exptype": null, "expamt": 1000 }, { "id": 121984, "expdate": null, "exptype": null, "expamt": 54 } ] [ { "id": 121982, "expdate": null, "exptype": null, "expamt": 1000 }, { "id": 121984, "expdate": null, "exptype": null, "expamt": 54}]

I want to sum both the expamt and should get the result as 1054 and should give the response with summed up amount alone if possible我想对两个expamt求和,结果应该是1054,如果可能的话,应该单独给出总和的响应

At first you should update API for /findTotSum endpoint according to your buisness logic.首先,您应该根据您的业务逻辑为/findTotSum端点更新 API。 If you want to get the sum of several values you should choose correct return type.如果你想得到几个值的总和,你应该选择正确的返回类型。

Second, you need to implement a service for such operations, in this case for sum operation, and use it controller class.其次,您需要为此类操作实现一个服务,在这种情况下用于求和操作,并使用它 controller class。

The response can be changed for something like that:响应可以更改为:

Class newResponseWithSum { 
  private List<SomeEntity> list;
  private int sum;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM