1

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
1
  • currently you're reading the length of json string. specify the data type as json, then jquery will parse the json string and return javascript object. Commented Nov 5, 2015 at 5:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.