Why This Javascript Is Not Returning The Restaurant Name?
Solution 1:
I think you can use console.log()
, alert()
or document.write()
as long as the information is deserialized, I am using JSON.parse()
for deserializing, eval()
is what people used before. Deserializing means to recover the data encoded in json so Javascript can use it as an object. In fact, if you use write.document(someJson) you will see [object object] meaning you have all your data there ready to be used, but you need to JSON.parse()
it first. JSON.stringnify()
is the opposite of what you want to do, because that function is for serialize the data and then transmit it, then the receptor needs to use JSON.parse()
to deserialized it and use it in Javascript.
I like to use write.document()
in order to see the information, but console log allows you to see what is inside of the object easier. Here, some code example:
// apiData would be the variable with the API information
var externalData = JSON.parse(apiData);
// will output [object object]
document.write(externalData);
// will output “ “
document.write(' ');
// will output the latitude
document.write(externalData.results[0].geometry.location.lat);
// will output in the console, you need to type externalData in the console log
console.log(externalData);
I hope this helps.
Just for clarification I used this answer to respond a similar question in this other forum How to get geocode latitude and longitude from json google map api in variables by javascript?)
Post a Comment for "Why This Javascript Is Not Returning The Restaurant Name?"