【data直接返回json数组】
今天公司前端,让我 data中不要返回json字符串(以{}
开始),而是返回json数组(以[]
开始)。
如下:
{
"msg": "操作成功",
"code": 200,
"data": [
{
"macId": 80,
"TimeList": [
{
"machineStatus": 0,
"addTimeStamp": 1638288011349
},
{
"machineStatus": 0,
"addTimeStamp": 1638374385410
}
]
},
{
"macId": 74,
"TimeList": [
{
"machineStatus": 0,
"addTimeStamp": 1638288011349
},
{
"machineStatus": 0,
"addTimeStamp": 1638374385410
}
]
}
]
}
行吧,就改呗。
原代码:
public AjaxResult qryStatusList(StatusQueryInfo queryInfo){
List<BuStatusList> list = statusListService.selectListByMacIds(info);
return AjaxResult.success(list );
}
改后的代码:
public AjaxResult qryStatusList(StatusQueryInfo queryInfo){
Long[] macIds = queryInfo.getMacIds();
Long beginTime = queryInfo.getBeginTime();
Long endTime = queryInfo.getEndTime();
List<Object> ls = new ArrayList();
for (Long macId : macIds) {
StatusQueryInfo info = new StatusQueryInfo();
info.setMacId(macId);
info.setBeginTime(beginTime);
info.setEndTime(endTime);
List<BuStatusList> list = statusListService.selectListByMacId(info);
Map map = new HashMap<>();
map.put("macId",macId);
map.put("TimeList",list);
ls.add(map);
}
return AjaxResult.success(ls);
}
其实要想返回json数组,那么return回去的,是个list集合就可以了。