使用 jquery 获取 json 对象中的元素数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13907048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:04:58  来源:igfitidea点击:

Get the number of elements in a json object using jquery

jqueryjson

提问by mjsey

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"      :".95",
        "description":"two of our famous Belgian Waffles with plenty of real maple syrup",
        "calories"   :"650"
    },
    {
        "name"       :"Strawberry Belgian Waffles",
        "price"      :".95",
        "description":"light Belgian waffles covered with strawberries and whipped cream",
        "calories"   :"900"
    },
    {
        "name"       :"Berry-Berry Belgian Waffles",
        "price"      :".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 未知,我如何获得返回元素的总数?

回答by nnnnnn

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 doknow that there will be exactly one property and that property will be an array you can do this:

编辑:在评论中,您说您不知道该属性被调用food- 如果您确实知道将有一个属性并且该属性将是一个数组,您可以执行以下操作:

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

If you don'tknow 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?

如果您知道只有一个属性,那么您的要求就太模糊了:如果可能有多个属性,您希望检查哪个属性的长度?

回答by ahren

var json = '{"food":[{"name":"Belgian Waffles","price":".95","description":"two of our famous Belgian Waffles with plenty of real maple syrup","calories":"650"},{"name":"Strawberry Belgian Waffles","price":".95","description":"light Belgian waffles covered with strawberries and whipped cream","calories":"900"},{"name":"Berry-Berry Belgian Waffles","price":".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/

演示: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。

回答by defau1t

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 属性来获取计数。应在食品项目上调用此属性。由于 food 已返回三个项目,因此您可以使用data.food.length;data 是全局对象。

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 有效。虽然它看起来有效,但看起来有手动换行符,在这种情况下它会失败: