我有一张如下所示的 map
Map<String,List<status>> statusmap=new HashMap<String,List<status>>();
因此,当我将此 map 输出为 JSON 数据时,我会执行以下操作:
return new ResponseEntity<Map<String,List<status>>>(statusmap,HttpStatus.OK);
给出了下面的 JSON 数据。
{"status":[{"connection":"OK","message":"Success"}]}
如上所述,我还有另一张 map
Map <String,List<Model>> modelmap= new HashMap<String,List<Model>>();
因此,当我将此 map 输出为 JSON 数据时,我会执行以下操作:
return new ResponseEntity<Map <String,List<Model>>>
给出了下面的 JSON 数据。
{"apidata":[{"id":1,"score":8},{"id":2,"score":6},{"id":3,"score":5}]}.
现在我希望将这两个数据结合起来
{"status":[{"connection":"OK","message":"Success"}],"apidata":[{"id":1,"score":8},{"id":2,"score":6},{"id":3,"score":5}]}
我尝试过 Map<Map<String,List<status>>,Map <String,List<Model>>> result=new HashMap <Map<String,List<status>>,Map <String,List<Model>>>();
result.put(statusmap,modelmap);
但它不是所需的格式。 如有任何帮助,我们将不胜感激。
最佳答案
您有两张 map :
Map<String,List<status>> statusmap=new HashMap<String,List<status>>();
Map<String,List<Model>> modelmap= new HashMap<String,List<Model>>();
按如下方式组合它们:
Map<String, Object> combinedMap = new HashMap<String, Object>();
combinedMap.putAll(statusmap);
combinedMap.putAll(modelmap);
您可能需要进行一些额外的转换(未经测试的代码)。然后只需使用 ResponseEntity 即可:
ResponseEntity<Map<String, Object>>();
关于java - 如何将两个map合并成一个json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320918/