user7477092
user7477092

Reputation:

How to combine two map into a json data

I have a map which is like below

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

So when I output this map as a JSON data then I do the following:

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

which gives the below JSON data.

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

As above I also have another map

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

So when I output this map as a JSON data then I do the following:

return new ResponseEntity<Map <String,List<Model>>>

which gives the below JSON data.

 {"apidata":[{"id":1,"score":8},{"id":2,"score":6},{"id":3,"score":5}]}.

Now I want the both data to be combined in the sense

{"status":[{"connection":"OK","message":"Success"}],"apidata":[{"id":1,"score":8},{"id":2,"score":6},{"id":3,"score":5}]}

I tried with Map<Map<String,List<status>>,Map <String,List<Model>>> result=new HashMap <Map<String,List<status>>,Map <String,List<Model>>>(); result.put(statusmap,modelmap);

But It did not come as the wanted format. Any help is appreciated.

Upvotes: 1

Views: 643

Answers (1)

john16384
john16384

Reputation: 8064

You have two maps:

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

Combine them as follows:

Map<String, Object> combinedMap = new HashMap<String, Object>();

combinedMap.putAll(statusmap);
combinedMap.putAll(modelmap);

You may need to do some extra casting (untested code). Then just use a ResponseEntity like:

ResponseEntity<Map<String, Object>>();

Upvotes: 1

Related Questions