PDA

View Full Version : Not able to solve the problem with NULL Mapping field while reading the JSON



thangamalar
11 Aug 2010, 2:56 AM
I have a JSON store with nested fields. FOr eaxmple some field will have 2 values and some will ahve one value only. The problem comes when I try to read the 2nd field value from the array for the ones it's not there. Please find the below JSON for example and marked it with comments in RED color.

{
"clusters": [
{
"hvProfile": "vmi-ci-2nic",
"netmask": 24,
"network": "1.1.1.1",
"region": "ETC",
"vlans": [
{
"netmask": 24,
"network": "2.2.2.2",
"vlan": 974
}, // Here I have 2 values fro vlans and if I have same across the JSON, it works fine. But some cases only one is there and my page is not loading
{
"netmask": 24,
"network": "3.3.3.3",
"vlan": 975
}
]
},{
"hvProfile": "onenic-onevlan",
"netmask": 25,
"network": "4.4.4.4",
"region": "ETC",
"vlans": [{
"netmask": 25,
"network": "5.5.5.5", // Only one vlans are there and this creates trouble when I read form the store mapping
"vlan": 0
}]
],
"limit": 100,
"totalCount": 17
}
}
Below is my EXT Store code.



store:new Ext.data.JsonStore({
id:'clusters'
,totalProperty:'totalCount'
,root:'clusters'
,url:listClusters.jsp'
,autoLoad : {params:{start:0, limit:100}}
,fields:[
{name:'subnet', mapping:'network'}
,{name:'netmask', mapping:'netmask'}
,{name:'profile', mapping:'hvProfile'}
,{name:'region', mapping:'region'}
,{name:'prodsubnet', mapping:'vlans[0].network'}
,{name:'prodnetmask', mapping:'vlans[0].netmask'}
,{name:'prodvlan', mapping:'vlans[0].vlan'}
,{name:'backupsubnet', mapping:'vlans[1].network' } // If I have the value present in all fields, it works fine. But some cases this value is empty. I tried something like
,{name:'backupsubnet', mapping:'vlans[1].network' !== null ? 'vlans[1].network' : ''}. But don't know if this is the right approach and it doesn't work as well.
]
})


Please help me out. Thanks.

Animal
11 Aug 2010, 3:16 AM
Use a convert function.

Or a custom data type.

Both well covered in the API.

Condor
11 Aug 2010, 3:16 AM
On newer Ext versions (3.1+ IIRC):

mapping: function(r){return r.vlans[1] ? r.vlans[1].network : '';}
or on older Ext versions (where mapping can't be a function):

mapping: 'vlans[1]', convert: function(v){return v ? v.network : '';}

thangamalar
11 Aug 2010, 3:19 AM
Thanks Condor. I saw your reply @ http://www.sencha.com/forum/showthread.php?80061-JSON-JsonStore-mapping-field-doesn-t-work and fixed my problem. Thanks a lot.

thangamalar
11 Aug 2010, 3:20 AM
Thanks guys. You guys have replied for the same reason in another thread and I found from there.

http://www.sencha.com/forum/showthread.php?80061-JSON-JsonStore-mapping-field-doesn-t-work

thangamalar
11 Aug 2010, 3:26 AM
Thanks guys.