10

I'm working with some Json that is similar to:

{ 
   "Apps" : [
   { 
     "Name" : "app1",
     "id" : "1",
     "groups" : [ 
       { "id" : "1", "name" : "test group 1", "desc" : "this is a test group" },
       { "id" : "2", "name" : "test group 2", "desc" : "this is another test group" } 
     ]
   }
   ]
}

If I parse it in jquery to return an object and I'm iterating through the apps like so:

$.each(myJsonObject.Apps, function() { ... };

How can I get the length of this.groups ?

I've tried

  • this.groups.length
  • this.groups.length()
  • $(this).groups.length()

and I cannot find any decent documentation on multiple tier json objects that have good information.

Any links or examples or suggestions would be appreciated.

4
  • 2
    Isn't that just a typo? groups in the JSON is all lowercase, but your code samples have Groups. Commented Apr 7, 2011 at 19:56
  • even in the correct case it does not work. ill update my example Commented Apr 7, 2011 at 19:58
  • Are you sure? It works fine when I just try it out in the console. Commented Apr 7, 2011 at 20:01
  • try $(var.Apps[0].groups).length Commented May 1, 2012 at 9:19

6 Answers 6

19

Try:

$.each(myJsonObject.Apps, function(i, obj) { ... });

obj.Groups.length;
Sign up to request clarification or add additional context in comments.

1 Comment

odd it doesnt work when i reference this... but if I reference myJsonObject it works. ill accept once the limit expires ><
4

Javascript is case sensitive. Change this.Groups.length to this.groups.length.

This code should work:

$.each(myJsonObject.Apps, function() { 
   alert(this.groups.length);
}); 

I have a working example on JSFiddle

To avoid this sort of problem, you should use a consistent capitalization. In Javascript, it is conventional to use camelCase.

Comments

2

this works well:

HTML:

<span id="result"></span>

JS:

var myJsonObject = 
{ 
   "Apps" : [
   { 
     "Name" : "app1",
     "id" : "1",
     "groups" : [ 
       { "id" : "1", "name" : "test group 1", "desc" : "this is a test group" },
       { "id" : "2", "name" : "test group 2", "desc" : "this is another test group" } 
     ]
   }
   ]
};

$.each(myJsonObject.Apps, function(i, el) { 
    $("#result").html(el.groups.length);
});

example

Comments

0

Looks like you just have a simple typo in your code. JavaScript is case-sensitive, so groups is not the same as Groups.

If your JSON object has groups in all lowercase, like your example, then you only need to use this.groups.length inside the iteration function.

Comments

0

Try this: function objectCount(obj) {

objectcount = 0;
$.each(obj, function(index, item) {
    objectcount = objectcount + item.length;
});
return objectcount;
}
objectCount(obj);

where obj is a json object with json array as sub objects

Comments

0

Try this

import org.json.JSONObject;

JSONObject myJsonObject = new JSONObject(yourJson);
int length = myJsonObject.getJSONArray("groups").length();

Comments

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.