javascript - How to get the right count NOT character length of JSON data - Stack Overflow

My code returns a JSON array, I think. The returned JSON array is stored in a javascript variable: resu

My code returns a JSON array, I think. The returned JSON array is stored in a javascript variable: result. If I

console.log(result); 

in FF, I get the output

[{"id":"G24","value":"Zas, S"},{"id":"G75","value":"Wara, TS"},{"id":"G48","value":"Jala, S"}]

Validated on jsonLint to be correct json.

In my code, if I count the number of elements in the array like so:

var key, count = 0;
for(key in result) 
{
  count++;
}
console.log("result count is:" + count); 

The output is 94 which is the length/count of characters in the array - [the sample output shown above is modified]

However, in the JSON tab in FF, it shows the result as being an array of objects:

0   Object { id="G24", value="Zas, S"}
1   Object { id="G75", value="Wara, TS"}
2   Object { id="G48", value="Jala, S"}

I have used alternative code pieces from 'stackoverflow' sources

for ( property in result )
{
   if(result.hasOwnProperty(property))
   {
     count++;
   }
}

And this has had the same oute. How can I have iterate rightly over this array or array of objects or string or whatever else it is? And get the count please?. Thanks.

My code returns a JSON array, I think. The returned JSON array is stored in a javascript variable: result. If I

console.log(result); 

in FF, I get the output

[{"id":"G24","value":"Zas, S"},{"id":"G75","value":"Wara, TS"},{"id":"G48","value":"Jala, S"}]

Validated on jsonLint to be correct json.

In my code, if I count the number of elements in the array like so:

var key, count = 0;
for(key in result) 
{
  count++;
}
console.log("result count is:" + count); 

The output is 94 which is the length/count of characters in the array - [the sample output shown above is modified]

However, in the JSON tab in FF, it shows the result as being an array of objects:

0   Object { id="G24", value="Zas, S"}
1   Object { id="G75", value="Wara, TS"}
2   Object { id="G48", value="Jala, S"}

I have used alternative code pieces from 'stackoverflow' sources

for ( property in result )
{
   if(result.hasOwnProperty(property))
   {
     count++;
   }
}

And this has had the same oute. How can I have iterate rightly over this array or array of objects or string or whatever else it is? And get the count please?. Thanks.

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Jul 30, 2012 at 9:34 pi.pi. 1,4813 gold badges19 silver badges26 bronze badges 1
  • Once you parsed the JSON, you can get the length of the array with data.length. See developer.mozilla/en/JavaScript/Guide/… – Felix Kling Commented Jul 30, 2012 at 9:38
Add a ment  | 

2 Answers 2

Reset to default 4

It sounds like you have an HTTP response returning a JSON document.

In the JSON tab, this is shown as JSON.

If your code, you are just taking the text of the response and operating on that.

You need to parse the JSON to create JavaScript objects.

Pass the string through JSON.parse and use json2.js to polyfill for older browsers.

You have to parse the JSON to create a JavaScript Array.

result = JSON.parse(result); // Now it's an array
console.log(result.length) // prints now what you want

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745663926a4639007.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信