Json 对象数组长度
我正在使用一些类似于以下内容的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试:
Try:
JavaScript 区分大小写。将
this.Groups.length
更改为this.groups.length
。此代码应该可以工作:
我在 JSFiddle 上有一个工作示例
为了避免此类问题,您应该使用一致的大写。在 Javascript 中,通常使用驼峰命名法。
Javascript is case sensitive. Change
this.Groups.length
tothis.groups.length
.This code should work:
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.
这效果很好:
HTML:
JS:
示例
this works well:
HTML:
JS:
example
看起来您的代码中只是有一个简单的拼写错误。 JavaScript 区分大小写,因此
groups
与Groups
不同。如果您的 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 asGroups
.If your JSON object has
groups
in all lowercase, like your example, then you only need to usethis.groups.length
inside the iteration function.试试这个:
function objectCount(obj) {
其中 obj 是一个 json 对象,以 json 数组作为子对象
Try this:
function objectCount(obj) {
where obj is a json object with json array as sub objects
试试这个
Try this