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?
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);
USE len() TO COUNT THE ITEMS IN A JSON OBJECT. Call len(obj) to return the number of items in a JSON object obj.
To obtain the length of a JSON-encoded array, use the json_array_length function.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With