最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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
Post a comment

comment list (0)

  1. No comments so far