Possible Duplicate:
Find length (size) of an array in jquery可能重复:在jquery中查找数组的长度(大小)
I have a json object returned from a php file as follows:-
我有一个从php文件返回的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"
}
]}
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?
这基本上是从物体返回三个食物元素。如果json数据是动态的并且使用jquery不知道,我怎样才能得到返回的元素总数?
3 个解决方案
#1
15
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:
假设您已经解析了JSON(或者jQuery已经为您完成了它,如果您使用它的Ajax函数它将会这样做)并且您将结果对象放在变量中,只需执行以下操作:
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.)
(注意:没有JSON对象这样的东西:在解析JSON之前它是一个字符串;在你解析它之后它就是一个对象。)
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?
如果您不知道将只有一个属性,那么您的要求太模糊:如果可能有多个属性,您想要检查其长度?
#2
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.
请注意,IE8及以下版本没有JSON的原生支持,但有一个polyfill可用。
Also, as another note, jQuery isn't doing the work here - this is just plain old javascript.
另外,作为另一个注释,jQuery没有在这里工作 - 这只是普通的旧javascript。
#3
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?
如果json数据是动态的并且使用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.
您可以使用length属性来获取计数。应该在食品上调用此属性。由于食物有三种物品,你可以使用data.food.length;数据是全局对象。
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:
还要确保您的JSON有效。虽然它似乎有效,但看起来有手动换行符,在这种情况下它会失败:
#1
15
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:
假设您已经解析了JSON(或者jQuery已经为您完成了它,如果您使用它的Ajax函数它将会这样做)并且您将结果对象放在变量中,只需执行以下操作:
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.)
(注意:没有JSON对象这样的东西:在解析JSON之前它是一个字符串;在你解析它之后它就是一个对象。)
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?
如果您不知道将只有一个属性,那么您的要求太模糊:如果可能有多个属性,您想要检查其长度?
#2
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.
请注意,IE8及以下版本没有JSON的原生支持,但有一个polyfill可用。
Also, as another note, jQuery isn't doing the work here - this is just plain old javascript.
另外,作为另一个注释,jQuery没有在这里工作 - 这只是普通的旧javascript。
#3
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?
如果json数据是动态的并且使用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.
您可以使用length属性来获取计数。应该在食品上调用此属性。由于食物有三种物品,你可以使用data.food.length;数据是全局对象。
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:
还要确保您的JSON有效。虽然它似乎有效,但看起来有手动换行符,在这种情况下它会失败: