如何将两个映射组合成一个json数据

时间:2017-04-10 10:35:14

标签: java json api

我有一张如下图所示的地图

Map<String,List<status>> statusmap=new HashMap<String,List<status>>();

因此,当我将此地图输出为JSON数据时,我会执行以下操作:

return new ResponseEntity<Map<String,List<status>>>(statusmap,HttpStatus.OK);

,它提供了以下JSON数据。

{"status":[{"connection":"OK","message":"Success"}]}

如上所述,我还有另一张地图

Map <String,List<Model>> modelmap= new HashMap<String,List<Model>>();

因此,当我将此地图输出为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);

但它没有成为想要的格式。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

你有两张地图:

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>>();