Json 对象数组长度

发布于 2024-10-31 08:28:18 字数 693 浏览 1 评论 0原文

我正在使用一些类似于以下内容的 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" } 
     ]
   }
   ]
}

如果我在 jquery 中解析它以返回一个对象,并且我正在像这样迭代应用程序:

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

How can I get the length of this.groups

我已经尝试过

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

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

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

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

{ 
   "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:

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

How can I get the length of this.groups ?

I've tried

  • 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.

Any links or examples or suggestions would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坠似风落 2024-11-07 08:28:18

尝试:

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

obj.Groups.length;

Try:

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

obj.Groups.length;
音盲 2024-11-07 08:28:18

JavaScript 区分大小写。将 this.Groups.length 更改为 this.groups.length

此代码应该可以工作:

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

我在 JSFiddle 上有一个工作示例

为了避免此类问题,您应该使用一致的大写。在 Javascript 中,通常使用驼峰命名法。

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

This code should work:

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

I have a working example on JSFiddle

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

过期情话 2024-11-07 08:28:18

这效果很好:

HTML:

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

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);
});

示例

this works well:

HTML:

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

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

她比我温柔 2024-11-07 08:28:18

看起来您的代码中只是有一个简单的拼写错误。 JavaScript 区分大小写,因此groupsGroups不同

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

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

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

只有影子陪我不离不弃 2024-11-07 08:28:18

试试这个:
function objectCount(obj) {

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

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

Try this:
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

梦与时光遇 2024-11-07 08:28:18

试试这个

import org.json.JSONObject;

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

Try this

import org.json.JSONObject;

JSONObject myJsonObject = new JSONObject(yourJson);
int length = myJsonObject.getJSONArray("groups").length();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文