6

Possible Duplicate:
Find length (size) of an array in jquery

I have a json object returned from a php file as follows:-

{"food":[
    {
        "name"       :"Belgian Waffles",
        "price"      :"$5.95",
        "description":"two of our famous Belgian Waffles with plenty of real maple syrup",
        "calories"   :"650"
    },
    {
        "name"       :"Strawberry Belgian Waffles",
        "price"      :"$7.95",
        "description":"light Belgian waffles covered with strawberries and whipped cream",
        "calories"   :"900"
    },
    {
        "name"       :"Berry-Berry Belgian Waffles",
        "price"      :"$8.95",
        "description":"light Belgian waffles covered with an assortment of fresh berries and whipped cream",
        "calories"   :"900"
    }
]}

This is essentially returning three food elements from the object. How can I get a count of the total number of elements being returned if the json data is dynamic and not known using jquery?

Community
  • 1
  • 1
mjsey
  • 1,938
  • 5
  • 18
  • 28

3 Answers3

16

Assuming you've parsed the JSON (or jQuery has done it for you, which it will if you use its Ajax functions) and you have the resulting object in a variable, just do this:

var data = /* your parsed JSON here */
var numberOfElements = data.food.length;

(Note: There's no such thing as a JSON object: before you parse JSON it's a string; after you parse it it's an object.)

EDIT: In a comment you said you wouldn't know that the property is called food - if you do know that there will be exactly one property and that property will be an array you can do this:

var data = /* your object */
var propName;
var numberOfElements;
for (propName in data);
numberOfElements = propName ? data[propName].length : 0;

If you don't know that there will be exactly one property then your requirement is too vague: if there might be multiple properties which one would you want to check the length of?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • 1
    You're welcome. Note that the `? :` test on my last line of code was to allow for a possible empty object with no properties. If you know there will always be a property just say `numberOfElements = data[propName].length;`... – nnnnnn Dec 17 '12 at 01:14
6
var json = '{"food":[{"name":"Belgian Waffles","price":"$5.95","description":"two of our famous Belgian Waffles with plenty of real maple syrup","calories":"650"},{"name":"Strawberry Belgian Waffles","price":"$7.95","description":"light Belgian waffles covered with strawberries and whipped cream","calories":"900"},{"name":"Berry-Berry Belgian Waffles","price":"$8.95","description":"light Belgian waffles covered with an assortment of fresh berries and whipped cream","calories":"900"}]}';

var obj = JSON.parse(json);

alert(obj.food.length);​

DEMO: http://jsfiddle.net/XhG6L/

Note that IE8 and below don't have native support for JSON, but there is a polyfill available.

Also, as another note, jQuery isn't doing the work here - this is just plain old javascript.

ahren
  • 16,803
  • 5
  • 50
  • 70
  • This is all well and good but for dynamic purposes I may not know the element "food"? – mjsey Dec 17 '12 at 01:01
  • You mean to say, you wouldn't know the property name? – ahren Dec 17 '12 at 01:02
  • 2
    I would suggest that you reexamine the structure then. More consistent keys will makes things much easier down the road. Maybe something like {categoryName: "food", items: [{name: '',}, ...]} – James S Dec 17 '12 at 01:09
0

How can I get a count of the total number of elements being returned if the json data is dynamic and not known using jquery?

You can use length property to get the count. This property should be invoked upon the food item. Since food has returns three items, you could use data.food.length; data is the global object.

Also make sure your JSON is valid. Though it seems valid, but looks like there are manual line breaks and in which case it will fail:

defau1t
  • 10,593
  • 2
  • 35
  • 47