【问题标题】:Length of a json value?json值的长度?
【发布时间】:2013-07-03 11:59:39
【问题描述】:

给定数据:

var json = [ 
  { "id": "A", "status":   1, "rank": 30,  "score": 0.1  },
  { "id": "B", "status":   1, "rank": 30,  "score": 0.7  },
  { "id": "C", "status":   1, "rank": 60,  "score": 0.8  },
  { "id": "AB", "status":  1, "rank": 160, "score": 0.6  },
  { "id": "ABC", "status": 1, "rank": 400, "score": 0.2 }
];

如何获取id的长度?

小提琴:http://jsfiddle.net/RBSC9/


以下不起作用:

var l = (json[3].id).text().length();
console.log("l = "+ l );

【问题讨论】:

  • 你在问如何获取字符串的长度???
  • 是的...是错字。

标签: javascript jquery json string-length


【解决方案1】:

请参考更新小提琴链接http://jsfiddle.net/RBSC9/3/

var l = (json[3].id).length

你想提到length,比如length;而不是length();

【讨论】:

    【解决方案2】:
    var len = json[3].id.length;
    console.log("len = "+ len);
    

    【讨论】:

      【解决方案3】:

      使用 *.length

      var l = json[3].id.length;
      console.log("length["+json[3].id+"] = "+ l );
      

      http://jsfiddle.net/RBSC9/2/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-12
        • 1970-01-01
        • 1970-01-01
        • 2016-11-28
        • 2011-11-17
        • 2013-05-27
        • 2014-10-18
        • 2011-03-15
        相关资源
        最近更新 更多