Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
Is that you want to have a list of objects for the key "events"? if yes, then you should place your list of date-event inside an array [] –  autobot_101 Dec 30 '13 at 6:44
    
i want json array count per date –  android_developer Dec 30 '13 at 6:48

4 Answers 4

up vote 3 down vote accepted

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()
    }
}
share|improve this answer
    
Works perfect thanks :) –  android_developer Dec 31 '13 at 6:09
  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
             }
share|improve this answer
    
gives exception: org.json.JSONException: Value json of type java.lang.String cannot be converted to JSONObject –  android_developer Dec 30 '13 at 7:01
    
Oh, ur is ur data comes like this only –  sush Dec 30 '13 at 7:03
    
look at my updated ans –  sush Dec 30 '13 at 7:16

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
        }
    }
}
share|improve this answer
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.