Jojo C Jojo C - 2 years ago 115
Ajax Question

Store json response to object so can get the value from the object

I am trying the get the geonames' API which will response JSON data.
I create a dataStorage Object and put the response in it.
Currently I have problem when getting the value from the storing object.

Here is my code:

var dataStorage = new Object();

function CountryQuery(geoId, geoCode) {
type: "GET",
url: "" + geoId + "&username=tompi",
dataType: "json",
success: function(data) {
dataStorage[geoCode] = data;

if (!('AN' in dataStorage)) {
CountryQuery(6255152, "AN");


The JSON response is look like below:

"name":"Bouvet Island",
"countryName":"Bouvet Island"

Answer Source

Try this :


instead of:

dataStorage[geoCode] = data;

and if you are looking for 'AN' as country code then your query will further not work..You will have to iterate through the JSON and check for the values.. in only checks the keys..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download