gpt4 book ai didi

java - 三个物体合而为一?

转载 作者:行者123 更新时间:2023-12-02 09:42:58 25 4
gpt4 key购买 nike

遇到一个问题,我返回三个 json 对象。无论如何,我想做的就是作为一个 JSON 对象返回。与我下面的结果类似。所以,我试图展示我的 Controller - 我相信我的 Controller 返回 3 个对象而不是 1 个。感谢您的帮助。

这是我的代码:

我尝试这样做:

@RequestMapping(value = "all", method = RequestMethod.GET)
public @ResponseBody
MyAutoComplete getMoney(@RequestParam(value = "myautocomplete") String myautocompleteValue) {

List<MyAutoComplete> list = myautoCompleteService.gettingMoney(myautocompleteValue);
return merge(list);

}


MyAutoComplete merge(List<MyAutoComplete> list){
MyAutoComplete accountacy = list.get(0);
for (int i = 1; i < list.size(); i++) {
MyAutoComplete myautoComplete = list.get(i);
if (myautoComplete.getTimeAnnual() != null) {
accountacy.setTimeAnnual((autoComplete.getTimeAnnual(());
}
if (autoComplete.getTimeBiweekly() != null) {
accountacy.setTimeBiweekly(autoComplete.getTimeBiweekly());
}
if (autoComplete.getTimeHourly() != null) {
accountacy.setTimeHourly(autoComplete.getTimeHourly());
}

}
return accountacy;
}

最佳答案

如果无法更改存储库端的查询,可以将列表中的对象合并到 Controller 中的单个对象

public @ResponseBody
LIst<AutoComplete> getSalary(@RequestParam(value = "autocomplete") String autocompleteValue) {

List<AutoComplete> list = autoCompleteService.retrieveSalary(autocompleteValue);
return Arrays.asList(merge(list));

}


AutoComplete merge(List<AutoComplete> list){
AutoComplete acc = list.get(0);
for (int i = 1; i < list.size(); i++) {
AutoComplete autoComplete = list.get(i);
if (autoComplete.getAnnual() != null) {
acc.setAnnual(autoComplete.getAnnual());
}
if (autoComplete.getBiweekly() != null) {
acc.setBiweekly(autoComplete.getBiweekly());
}
if (autoComplete.getHourly() != null) {
acc.setHourly(autoComplete.getHourly());
}

}
return acc;
}

关于java - 三个物体合而为一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com