0

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?

dilemunal
  • 19
  • 5
  • If I understand you correct, "list1Repo.countAll()" this is returning a number then why are you storing it into a List? – AkSh Feb 15 '22 at 14:19
  • Yes , I am open to any suggestions. I don't have to store them in a list. – dilemunal Feb 15 '22 at 14:21
  • Does this answer your question? [How to convert hashmap to JSON object in Java](https://stackoverflow.com/questions/12155800/how-to-convert-hashmap-to-json-object-in-java) – pringi Feb 15 '22 at 14:25
  • No actually, the main problem is that I need to count datas in three repositories and put them in a list or a map. Then I need to convert it to json, it solves the second part but not very helpful for the first one. – dilemunal Feb 15 '22 at 14:29

1 Answers1

1

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.

AkSh
  • 218
  • 1
  • 7