jQuery Json 对象数组长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5586793/
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 19:25:31  来源:igfitidea点击:

Json Object Array Length

jqueryarraysjson

提问by Patrick

I'm working with some Json that is similar to:

我正在使用一些类似于以下内容的 Json:

{ 
   "Apps" : [
   { 
     "Name" : "app1",
     "id" : "1",
     "groups" : [ 
       { "id" : "1", "name" : "test group 1", "desc" : "this is a test group" },
       { "id" : "2", "name" : "test group 2", "desc" : "this is another test group" } 
     ]
   }
   ]
}

If I parse it in jquery to return an object and I'm iterating through the apps like so:

如果我在 jquery 中解析它以返回一个对象并且我正在遍历应用程序,如下所示:

$.each(myJsonObject.Apps, function() { ... };

How can I get the length of this.groups?

我怎样才能得到 的长度this.groups

I've tried

我试过了

  • this.groups.length
  • this.groups.length()
  • $(this).groups.length()
  • this.groups.length
  • this.groups.length()
  • $(this).groups.length()

and I cannot find any decent documentation on multiple tier json objects that have good information.

而且我找不到关于具有良好信息的多层 json 对象的任何体面的文档。

Any links or examples or suggestions would be appreciated.

任何链接或示例或建议将不胜感激。

回答by mattsven

Try:

尝试:

$.each(myJsonObject.Apps, function(i, obj) { ... });

obj.Groups.length;

回答by Peter Olson

Javascript is case sensitive. Change this.Groups.lengthto this.groups.length.

Javascript 区分大小写。更改this.Groups.lengththis.groups.length

This code should work:

此代码应该工作:

$.each(myJsonObject.Apps, function() { 
   alert(this.groups.length);
}); 

I have a working example on JSFiddle

我有一个关于JSFiddle的工作示例

To avoid this sort of problem, you should use a consistent capitalization. In Javascript, it is conventional to use camelCase.

为避免此类问题,您应该使用一致的大小写。在 Javascript 中,习惯上使用驼峰命名法。

回答by andres descalzo

this works well:

这很好用:

HTML:

HTML:

<span id="result"></span>

JS:

JS:

var myJsonObject = 
{ 
   "Apps" : [
   { 
     "Name" : "app1",
     "id" : "1",
     "groups" : [ 
       { "id" : "1", "name" : "test group 1", "desc" : "this is a test group" },
       { "id" : "2", "name" : "test group 2", "desc" : "this is another test group" } 
     ]
   }
   ]
};

$.each(myJsonObject.Apps, function(i, el) { 
    $("#result").html(el.groups.length);
});

example

例子

回答by Karthick Kumar

Try this: function objectCount(obj) {

试试这个: function objectCount(obj) {

objectcount = 0;
$.each(obj, function(index, item) {
    objectcount = objectcount + item.length;
});
return objectcount;
}
objectCount(obj);

where obj is a json object with json array as sub objects

其中 obj 是一个 json 对象,以 json 数组作为子对象

回答by Murshida Mushfique

Try this

尝试这个

import org.json.JSONObject;

JSONObject myJsonObject = new JSONObject(yourJson);
int length = myJsonObject.getJSONArray("groups").length();

回答by Dominic Barnes

Looks like you just have a simple typo in your code. JavaScript is case-sensitive, so groupsis notthe same as Groups.

看起来您的代码中只有一个简单的错字。JavaScript是大小写敏感的,所以groups一样的Groups

If your JSON object has groupsin all lowercase, like your example, then you only need to use this.groups.lengthinside the iteration function.

如果您的 JSON 对象groups全部为小写,就像您的示例一样,那么您只需要this.groups.length在迭代函数内部使用。