PDA

View Full Version : JSON to array



Absalom
17 Aug 2007, 8:05 AM
I've been trying to solve this for hours, can't figure it out.

I'm trying to populate an array from JSON data with no success. I was able to populate a combobox, so I know there's nothing wrong with the connection or the data.

I don't know how many solutions I've tried, could someone please post a little snippet to show how it's done?

JSON data:


({"total":"3","results":[{"id":"1","name":"User1"},{"id":"2","name":"User2"},{"id":"3","name":"User3""}]})


My latest attempt:


Ext.Ajax.request({
url : 'http://mydomain.com/getusers' ,
success: function ( resp, opt ) {
alert('Great success!'); // This alert is shown.
var results = Ext.util.JSON.decode(resp.data);
alert('result[0][0]: ' + results[0][0]); // This alert isnt.
},
failure: function ( resp, opt ) {
alert('Failure..');
}
});


This should be really really easy shouldn't it?

kimu
17 Aug 2007, 8:23 AM
Read more carefully the documentation and look the examples. Anyway, try this:


Ext.Ajax.request({
url : 'http://mydomain.com/getusers' ,
success: function ( resp, opt ) {
alert('Great success!'); // This alert is shown.
var results = Ext.util.JSON.decode(resp.responseText);
alert('result[0][0]: ' + results[0][0]); // This alert isnt.
},
failure: function ( resp, opt ) {
alert('Failure..');
}
});

resp -> the response object, has your json string in resp.responseText, not in resp.data.
result is an array with 2 elements result['total'] and result['result'] (which is an array itself).

Absalom
17 Aug 2007, 8:39 AM
Awsome! Thank you, thank you Kimu.