Reputation: 673
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:
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
Upvotes: 1
Views: 8182
Reputation: 12743
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()
}
}
Upvotes: 6
Reputation: 11188
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.
Upvotes: 1
Reputation: 3874
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
}
Upvotes: 0
Reputation: 4258
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
}
}
}
Upvotes: 0