PDA

View Full Version : JSON + Ajax interaction help!



Roger_K
21 Jul 2011, 5:04 AM
Hi,
I am doing an app that finds a user's data and display it.(Nothing illegal, just a proof of concept)
Anyway, I used to Ext.Ajax request to request for the information saved in a user_data.js file.

The problem is, when I try to access the user data, it gives me an 'undefined'.

Here's the code I use to run the Ajax Request:

Ext.Ajax.request({
url: 'json/user_data.js',
params: {
format: 'json',
},
method:'POST',
success:function(data){
var person=Ext.util.JSON.decode(data.responseText)
alert(person.names[0][2])

},
failure:function(){
alert('fail')
}
});

And here's my js file that contains the user data:

{
"names":[{
"John":[{"location":"Chillin in Russia!","bytes":1000000,"name":"John"}],
"Max":[{"location":"Chillin in Mexico!","bytes":1000000,"name":"Max"}]
}]

}

I am able to parse the JSON successfully but unable to display the information. In this case I wish to display John's name, but instead it gives me "undefined".

All help appreciated!

conorarmstrong
21 Jul 2011, 5:53 AM
your returned JSON doesn't have a person.names property

Hertz
21 Jul 2011, 6:00 AM
This works:


person.names[0]["name"][0].name

Example:


person.names[0]["John"][0].name

You should give a better structure to your JSON.