Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the number of elements in a json object using jquery [duplicate]

Tags:

json

jquery

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?

like image 694
mjsey Avatar asked Dec 17 '12 00:12

mjsey


People also ask

How to clone an object in jQuery?

Syntax: // Create a clone of the object using the extend() method let newObj = jQuery. extend({}, obj); // Create a deep clone of the object using the deep parameter let newDeepObj = jQuery. extend(true, {}, obj);

How do I count the number of elements in JSON?

USE len() TO COUNT THE ITEMS IN A JSON OBJECT. Call len(obj) to return the number of items in a JSON object obj.

How to get length of JSON array in jQuery?

To obtain the length of a JSON-encoded array, use the json_array_length function.

How do you find the number of elements in a JSON array?

JsonArray::size() gets the number of elements in the array pointed by the JsonArray . If the JsonArray is null, this function returns 0 . Internally, this function walks a linked-list to count the elements, so its time complexity is O(n). Don't use this function to create a for loop; instead, use iterators.


2 Answers

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?

like image 145
nnnnnn Avatar answered Oct 08 '22 15:10

nnnnnn


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.

like image 23
ahren Avatar answered Oct 08 '22 13:10

ahren