1

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

2
  • 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 [] Commented Dec 30, 2013 at 6:44
  • i want json array count per date Commented Dec 30, 2013 at 6:48

4 Answers 4

6

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()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
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.

Comments

0

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
        }
    }
}

Comments

0
  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
             }

2 Comments

gives exception: org.json.JSONException: Value json of type java.lang.String cannot be converted to JSONObject
Oh, ur is ur data comes like this only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.