Home > Software design >  count datas in three repositories and put them in a list or a map and convert it to json
count datas in three repositories and put them in a list or a map and convert it to json

Time:02-16

I have three lists that holds the number of data in a database

List<Long> list1= list1Repo.countAll();
List<Long> list2= list2Repo.countAll();
List<Long> list3= list3Repo.countAll();

I want to combine these lists into one JSON object which supposed to be as follows:

[
  { label: "list1", value: 30 },
  { label: "list2", value: 25 },
  { label: "list3", value: 25 },
 
]

value is the counted numbers inside the lists. How do i do this?

CodePudding user response:

Store your data like this in a Map:

Map<String, Long> listCountMap = new HashMap<>();
listCountMap.put("list1", list1Repo.countAll());
listCountMap.put("list2", list2Repo.countAll());
listCountMap.put("list3", list3Repo.countAll());

Then refer to this answer to convert your map into a JSON object.

  • Related