Bill Noble
Bill Noble

Reputation: 6744

JSON parsing incorrectly

I am trying to extract data from the result of a Google Places call.

If I run the following Javascript:

alert(JSON.stringify(places[0].geometry.location));

I get the following output, as expected:

{"lat":59.9138688,"lng":10.752245399999993}

However if I put .lat on the end to extract the lat value and run:

alert(JSON.stringify(places[0].geometry.location.lat));

I get:

undefined

And if I run the alert without the JSON.stringify as follows:

alert(places[0].geometry.location.lat);

I get:

function (){return a}

What am I doing wrong?

Upvotes: 1

Views: 47

Answers (2)

Roysh
Roysh

Reputation: 1550

JSON.stringify - Is a function that usually accepts an object (although, it can get more types). In your case, it takes a Number, since places[0].geometry.location.lat is a Number and not an Object.

Therefore, if you want to get a string you should use the .toString() function that comes with the Number prototype.

Try alert(places[0].geometry.location.lat.toString())

Upvotes: 2

AliBoronsi
AliBoronsi

Reputation: 764

You must do this:

alert(places[0].geometry.location.lat());

Upvotes: 1

Related Questions