Expand my Community achievements bar.

SOLVED

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

Avatar

Level 5

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 
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

2 Replies

Avatar

Employee Advisor

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

 

Avatar

Correct answer by
Community Advisor

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