Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

javascript - 获取json对象的大小(get size of json object)

i have a json object that gets returned by an AJAX request and I am having some trouble with the .length because it keeps returning undefined .

(我有一个json对象,由AJAX请求返回,我遇到一些.length因为它一直返回undefined 。)

Just wondering if I'm using it right:

(只是想知道我是否正确使用它:)

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

They both return undefined even 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"}}}
  ask by nkcmr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...