简体   繁体   中英

Count the json objects inside json Array in Android?

I am trying to parse json values into my Android application. Here i am stuck at a place wherein I need to have the objects length inside the json arraylist.

Below is my json:

    "events": { //json object
            "2012-11-30": [ // json array
                {
                    "privacy": "OPEN",
                    "name": "LOGDGE"   
                }
            ],
            "2013-08-17": [
                {
                    "privacy": "OPEN",
                    "name": "Party: Dinner and Dancing"
                }
            ],
            "2013-09-14": [
                {
                    "privacy": "OPEN",
                    "name": "Party: Dinner and Dancing"
                }
            ],
            "2013-09-27": [
                {
                    "privacy": "OPEN",
                    "name": "Salsa Party!"  
                }

            {
             "privacy": "OPEN",
                     "name": "Dance Performance Course",
            }       
            ],
            "2013-10-23": [
                {
                    "privacy": "OPEN",
                    "name": "Dance Performance Course"
                }
            ]
    }

Now my question is how do I parse to get the length of the json array like:

  • 2012-11-30 count = 1
  • 2013-08-17 count = 1
  • 2013-09-27 count = 2

How do I loop the json array using the date's as they are json object and find the length of each of the json array dates. I want count of each array individually over here. Would I need to add a key value kind of pair in the json array "dates" so as I can have a for loop to get the count of the values inside the array?

Thank you in advance

Try Below Code and count objects using length of JsonArray:

Iterator<Object> keys = eventobject.keys();
while (keys.hasNext()) {
  String key = keys.next();
    try{
         JSONArray array = jObject.getJSONArray(key);
         //count objects values using array length.
    }
    catch(Exception e){
        e.printStackTrace()
    }
}
String jstr = jso.getString("Events");
JSONObject mdcalkey = new JSONObject(jstr);
Iterator iter = mdcalkey.keys();
ArrayList<String> arr = new ArrayList<String>();
ArrayList<String> arrTimeDate = new ArrayList<String>();
ArrayList<String> arrtype = new ArrayList<String>();

while (iter.hasNext()) {
    arr.add((String) iter.next());
}

// ja = mdcalkey.getJSONArray(arr.get(0));
jar = new JSONArray[arr.size()];

This arr array will display all dates array..please check it..if any query then tell me.

  String json =  "events": { //json object
        "2012-11-30": [ // json array
            {
                "privacy": "OPEN",
                "name": "LOGDGE"   
            }
        ],
        "2013-08-17": [
            {
                "privacy": "OPEN",
                "name": "Party: Dinner and Dancing"
            }
        ],
        "2013-09-14": [
            {
                "privacy": "OPEN",
                "name": "Party: Dinner and Dancing"
            }
        ],
        "2013-09-27": [
            {
                "privacy": "OPEN",
                "name": "Salsa Party!"  
            }

        {
         "privacy": "OPEN",
                 "name": "Dance Performance Course",
        }       
        ],
        "2013-10-23": [
            {
                "privacy": "OPEN",
                "name": "Dance Performance Course"
            }
        ]
}
       try {
                JSONObject  obj = new getJSONObject(new JSONObject("{"+"json"+"}").getJSONObject("events").toString());
                Iterator iterator = obj.keys();
          while (iterator.hasNext())
         {
          String key = (String) iterator.next();
          JSONArray array = obj.optJSONArray(key);
          if (array != null)
         {
        // the value for this key is an array
          for (int i = 0; i < array.length; i++){
          // do stuff
           }
         }
         }
          }
       catch (JSONException e)
            {
            // handle exception here
             }

You can iterate over the keys in your JSONObject and check if their corresponding values are JSONArrays, then store their lengths in a map:

Iterator iter = yourJSONObject.keys();
while (iter.hasNext())
{
    String key = (String) iter.next();
    JSONArray array = yourJSONObject.optJSONArray(key);
    if (array != null)
    {
        // the value for this key is an array
        for (int i = 0; i < array.length; i++){
            // do stuff
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM