PDA

View Full Version : Mapping problem with JSON data



Designworxz
11 Oct 2011, 6:15 PM
Am getting JSON data from webservice but not able to Map it .
Response from service

{"Code":"xyz","Name":"John","Countries":[{"Code":"au","Description":"Australia"},{"Code":"ca","Description":"Canada"}]}


Model


Ext.regModel('Country', {
fields: [ 'Code','Description' ],
belongsTo:'User'
});


Ext.regModel('User', {
fields: ['Name','Code'],
associations: [
{type: 'hasMany', model: 'Country', name: 'Countries'}]

});


Store


Store = new Ext.data.Store({
model: 'User',
autoLoad:false,
proxy: {
type: 'ajax',
url : 'webservice',
reader: {
type: 'json'

}
}
});

bassjobsen
12 Oct 2011, 10:36 AM
Hi,

The json response from your server seems to missing a root or at least the data should be surround by square brackets.

Example response:

{"users": [{"Code":"xyz","Name":"John","Countries":[{"Code":"au","Description":"Australia"},{"Code":"ca","Description":"Canada"}]}

and:

proxy: {
type: 'ajax',
url : 'lib/test.json',
reader: {
type: 'json',
root: 'users'
}
}

I also wonder if you do need the associations cause you read from one source.
Countries becomes an array automatically.

maybe you can use:

Ext.regModel('User', {
fields: ['Name','Code',{name: 'Countries', type: 'auto'}]
});

see also: http://www.sencha.com/forum/showthread.php?144130-How-to-define-Array-Field-in-Ext-Model

Designworxz
13 Oct 2011, 4:11 PM
Thanks