Javascript 获取json对象的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6756104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:03:34  来源:igfitidea点击:

get size of json object

javascriptjson

提问by nkcmr

i have a json object that gets returned by an AJAX request and I am having some trouble with the .lengthbecause it keeps returning undefined. Just wondering if I'm using it right:

我有一个由 AJAX 请求返回的 json 对象,我遇到了一些问题,.length因为它一直在返回undefined。只是想知道我是否正确使用它:

console.log(data.length);
console.log(data.phones.length);

They both return undefinedeven though they are valid objects.

undefined即使它们是有效对象,它们也会返回。

Update:
Sample of the JSON object returned:

更新:
返回的 JSON 对象示例:

{"reqStatus":true,"phones":{"one":{"number":"XXXXXXXXXX","type":"mobile"},"two":{"number":"XXXXXXXXXX","type":"mobile"}}}

回答by Kasun

You can use something like this

你可以使用这样的东西

<script type="text/javascript">

  var myObject = {'name':'Kasun', 'address':'columbo','age': '29'}

  var count = Object.keys(myObject).length;
  console.log(count);
</script>

回答by tomfumb

Your problem is that your phones object doesn't have a length property (unless you define it somewhere in the JSON that you return) as objects aren't the same as arrays, even when used as associative arrays. If the phones object was an array it would have a length. You have two options (maybe more).

您的问题是您的手机对象没有 length 属性(除非您在返回的 JSON 中的某处定义它),因为对象与数组不同,即使用作关联数组也是如此。如果phones 对象是一个数组,它将有一个长度。您有两个选择(可能更多)。

  1. Change your JSON structure (assuming this is possible) so that 'phones' becomes

    "phones":[{"number":"XXXXXXXXXX","type":"mobile"},{"number":"XXXXXXXXXX","type":"mobile"}]
    

    (note there is no word-numbered identifier for each phone as they are returned in a 0-indexed array). In this response phones.lengthwill be valid.

  2. Iterate through the objects contained within your phones object and count them as you go, e.g.

    var key, count = 0;
    for(key in data.phones) {
      if(data.phones.hasOwnProperty(key)) {
        count++;
      }
    }
    
  1. 更改您的 JSON 结构(假设这是可能的),以便 'p​​hones' 变成

    "phones":[{"number":"XXXXXXXXXX","type":"mobile"},{"number":"XXXXXXXXXX","type":"mobile"}]
    

    (请注意,每个电话都没有字编号标识符,因为它们以 0 索引数组返回)。在此响应phones.length将是有效的。

  2. 遍历您的手机对象中包含的对象并在您进行时计算它们,例如

    var key, count = 0;
    for(key in data.phones) {
      if(data.phones.hasOwnProperty(key)) {
        count++;
      }
    }
    

If you're only targeting new browsers option 2 could look like this

如果您只针对新浏览器选项 2 可能如下所示

回答by ForestierSimon

Consider using underscore.js. It will allow you to check the size i.e. like that:

考虑使用underscore.js。它将允许您检查大小,即:

var data = {one : 1, two : 2, three : 3};
_.size(data);
//=> 3
_.keys(data);
//=> ["one", "two", "three"]
_.keys(data).length;
//=> 3

回答by kkk

you dont need to change your JSON format.

您不需要更改您的 JSON 格式。

replace:

代替:

console.log(data.phones.length);

with:

和:

console.log( Object.keys( data.phones ).length ) ;

回答by surya handoko

var json=[{"id":"431","code":"0.85.PSFR01215","price":"2457.77","volume":"23.0","total":"565.29"},{"id":"430","code":"0.85.PSFR00608","price":"1752.45","volume":"4.0","total":"70.1"},{"id":"429","code":"0.84.SMAB00060","price":"4147.5","volume":"2.0","total":"82.95"},{"id":"428","code":"0.84.SMAB00050","price":"4077.5","volume":"3.0","total":"122.32"}] 
var obj = JSON.parse(json);
var length = Object.keys(obj).length; //you get length json result 4

回答by Hafsal Rh

try this

尝试这个

$.parseJSON(data).length

回答by Mahesh Gawhane

use this one

使用这个

//for getting length of object
 int length = jsonObject.length();

or

或者

//for getting length of array
 int length = jsonArray.length();

回答by user2674838

$(document).ready(function () {
    $('#count_my_data').click(function () {
        var count = 0;
        while (true) {
             try {
                var v1 = mydata[count].TechnologyId.toString();
                count = count + 1;
            }
            catch (e)
            { break; }
        }
        alert(count);
    });
});