【问题标题】:How to combine two map into a json data如何将两个地图组合成一个json数据
【发布时间】:2017-04-10 10:35:14
【问题描述】:

我有一张像下面这样的地图

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

但它并不是想要的格式。 任何帮助表示赞赏。

【问题讨论】:

    标签: java json api


    【解决方案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>>();
    

    【讨论】:

    • 像魅力一样工作
    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多