JSON-查找对象的长度 [英] JSON - Find the length of objects

查看:95
本文介绍了JSON-查找对象的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

我正在设置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来获取annotation[0].length的长度.我得到了预期的结果,即:5.与用户"和用户"相同"id".

但是我没有得到单词","id58","id61"等的长度. 我也想知道comment&的长度. commentUser.

请帮助我.

解决方案

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

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

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

HI,

I'm having a JSON parsed return object set.

{
  "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"]
      }
  }

I want to know about the length of each object and array.

I've used .length to get the length of annotation[0].length. I'm getting the expected result i.e.: 5. The same is to "user" & "id".

But I'm not getting the lengths of "word", "id58", "id61", etc... Also I want to know about the length of the comment & commentUser.

Please help me in this.

解决方案

The value for the key word in your example (let's call it obj) is an array, so obj.word.length should be 2.

The value for comment and commentUser is an object, which does not have length per se, so you'll need to count them yourself:

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

这篇关于JSON-查找对象的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆