user2570937
user2570937

Reputation: 852

Jquery post loop through json data

I'm trying to loop through my data I received when doing a jquery .post however it's showing up as a much higher count than it is. What is making it say 109 as the length of data?

.done(function( data ) {
   console.log(data);
   console.log(data.length);
});

Below is the console logs:

[{"lat":33.115868,"lng":-117.186295},{"lat":33.117237,"lng":-117.186295},{"lat":33.111866,"lng":-117.186295}]

109

Upvotes: 1

Views: 151

Answers (2)

Suchit kumar
Suchit kumar

Reputation: 11859

you need to parse the encoded data using:JSON.parse or $.parseJSON.

var ndata=JSON.parse(data);
console.log(ndata.length);

Upvotes: 1

Tushar
Tushar

Reputation: 87203

Although, it looks like JSON, the data received from server is string. To convert it into JSON use JSON.parse before iterating over it.

Use

JSON.parse(data);

to get JSON object from the string.


OR use dataType: 'json' in ajax configuration.

Upvotes: 1

Related Questions