Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON-查找对象的长度_Javascript_Json - Fatal编程技术网

Javascript JSON-查找对象的长度

Javascript JSON-查找对象的长度,javascript,json,Javascript,Json,嗨 我有一个JSON解析的返回对象集 { "word":[ "offered", "postings" ], "annotation":[ ["offered highlighted","this is also given as annotation","This annotation has been added here currently","offering new annotation points","\"offered\" is

我有一个JSON解析的返回对象集

{
  "word":[
      "offered",
      "postings"
  ],

  "annotation":[
      ["offered highlighted","this is also given as annotation","This annotation has been added here currently","offering new annotation points","\"offered\" is in the sense of languages"],
      ["my postings","this is new annotation for postings.","my postings","this is posting annotation.... Working for the feature of the annotation."]
  ],

  "user":[
  ["","","vinoth","Anonymous","Vinoth"],
  ["Anonymous","vinoth","Anonymous","Arputharaj"]
  ],

  "id":[
  ["58","60","61","63","68"],
  ["32","57","59","62"]
  ],

  "comment":
  {
    "id58":["first comment","This is a old comment for this annotation","Next level of commenting.","Fourth comment to this annotation","testing"],
    "id61":["this is an old annotation.\r\nMy comment is very bad about this.","Second comment to this annotation"],
    "id57":["I want to add one more comment to this"]
    },

    "commentUser":{
      "id58":["vinoth","Anonymous","Vinothkumar","Vinothkumar","vinoth"],
      "id61":["Anonymous","Commentor"],
      "id57":["vinoth"]
      }
  }
我想知道每个对象和数组的长度

我已经使用
.length
获取了
注释[0]的长度。length
。我得到了预期的结果,即:5。“用户”和“id”也是如此

但我不知道“单词”、“id58”、“id61”等的长度。。。 我还想知道
注释
注释用户
的长度


请在这方面帮助我。

在您的示例中,关键字
word
的值(我们称之为
obj
)是一个数组,因此
obj.word.length
应该是2

comment
commentUser
的值是一个对象,本身没有长度,因此您需要自己计算它们:

var length=0;
for(var dummy in obj.comment) length++;

要计算对象具有的属性数,需要循环遍历属性,但请记住使用
hasOwnProperty
函数:

var count = 0;
for (var p in obj) {
    if (obj.hasOwnProperty(p)) {
        count++;
    }
}
如果您忘记这样做,您将在继承的属性中循环。如果您(或某个库)已为
对象的原型指定了一个函数,则所有对象似乎都具有该属性,因此看起来比它们本身“长”一项

为了避免需要记住这一点,考虑使用jQuery的<代码>每个< /代码>代替:

var count = 0;
$.each(obj, function(k, v) { count++; });
使用当前浏览器更新

Object.keys(someObj).length

谢谢你的回答。这适用于“评论”和“评论用户”。但是“myObj.word.length”返回“undefined”(myObj是我的对象)。@Vinothkumar Arputharaj-假设
myObj
具有示例代码中给出的定义,那么
myObj.word
是长度为2的数组。如果您发现
myObj.word.length
未定义的
,那么您对情况的某些描述是错误的。为什么不在浏览器中设置断点并检查数据?你可以在Firefox中使用Firebug或IE 8和Chrome的内置调试器来实现这一点。不过,我可以通过使用for循环计算**myObj.word.length**的长度。