Need to count the items present in same category in json. | Community
Skip to main content
Adobe Employee
July 19, 2022
Solved

Need to count the items present in same category in json.

  • July 19, 2022
  • 2 replies
  • 2571 views

Hi,

 

"Items": [
        {
            "date""2022-07-19T08:47:51.456Z",
            "user_id""admin",
            "category""Category 4",
            "title""Category 4 bookmark"
        },
        {
            "date""2022-07-19T08:47:51.456Z",
            "user_id""admin",
            "category""Category 3",
            "title""Category 3 bookmark"
        },
        {
            "date""2022-07-16T11:01:17.887Z",
            "bookmark_url""https://www.facebook.com/",
            "user_id""admin",
            "category""Category 4",
            "title""Category 4 Title"
        },
        {
            "date""2022-07-16T11:01:17.887Z",
            "bookmark_url""https://www.google.com/",
            "user_id""admin",
            "category""Category 3",
            "title""Category 3 Title"
        },
        {
            "date""2022-07-16T13:18:24.785Z",
            "bookmark_url""https://www.twitter.com/",
            "user_id""admin",
            "category""Category 1",
            "title""Category 1 Title"
        }
    ],
 
This is the response I'm getting to the api call I made. I need to List the category from each items and add the count of each category.
 
I have used jsonobject and json array to iterate through the items and get the category. I'm able get a count of each category. Is there any logic to get the count?
 
Thank you 
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by BrianKasingli

It really depends on the JAVA library you are using. Take some time to Google Java docs that correspond to the Java library you are using.

Assuming that you are using the org.json library, you can call on the size method, for example:

 

import org.json.simple.JSONArray; import org.json.simple.JSONObject; ... JSONArray array = new JSONArray(); array.add("e-mail: krishna_kasyap@gmail.com"); array.add("phone: 9848022338"); array.add("city: Hyderabad"); array.add("Area: Madapur"); array.add("State: Telangana"); Sytem.out.println(array.size());
 

2 replies

Mohit_KBansal
Adobe Employee
Adobe Employee
July 19, 2022

I would follow the following steps:

  1. Create a map <category name, count>
  2. Iterate JsonArray object
  3. add/increase map object for each category
    Your map will have list of categories with count
  4. now iterate over JsonArray again
  5. get category count from map, and add count in JsonObject

 

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
July 20, 2022

It really depends on the JAVA library you are using. Take some time to Google Java docs that correspond to the Java library you are using.

Assuming that you are using the org.json library, you can call on the size method, for example:

 

import org.json.simple.JSONArray; import org.json.simple.JSONObject; ... JSONArray array = new JSONArray(); array.add("e-mail: krishna_kasyap@gmail.com"); array.add("phone: 9848022338"); array.add("city: Hyderabad"); array.add("Area: Madapur"); array.add("State: Telangana"); Sytem.out.println(array.size());