PDA

View Full Version : JSON with parenthesis



yesakmac
12 Mar 2013, 3:03 PM
I'm taking some JSON output and loading it into a store. The JSON String is
{"totalCount":"3","Events":[{"EVENTNUM":"T00000000133","SUM(A.NUM_CUST)":61.0,"SUM(A.OUT_MINUTES)":13.0,"XPERS":796072.0,"OFF_DTS":"20130307084811CS","MAX(A.RESTORE_DTS)":"20130307090200CS","CAUSE":"10","FEEDER":"17FL012011","DEV_NAME":"T007F12127","WEATHER_CODE":"THUNDERSTORM","COMPLETION_REMARKS":"Testing Step Restore for Kasey"},{"EVENTNUM":"T00000000014","SUM(A.NUM_CUST)":61.0,"SUM(A.OUT_MINUTES)":4.0,"XPERS":796072.0,"OFF_DTS":"20130304135945CS","MAX(A.RESTORE_DTS)":"20130304140400CS","CAUSE":"09","FEEDER":"17FL012011","DEV_NAME":"T007F12127","WEATHER_CODE":"NORMAL FOR SEASON","COMPLETION_REMARKS":"COMPLETETION REMARKS FROM TRUCK ON TOFS. "},{"EVENTNUM":"T00000000173","SUM(A.NUM_CUST)":61.0,"SUM(A.OUT_MINUTES)":3.0,"XPERS":79607.0,"OFF_DTS":"20130307161424CS","MAX(A.RESTORE_DTS)":"20130307161800CS","CAUSE":"01","FEEDER":"17FL012011","DEV_NAME":"T007F12127","WEATHER_CODE":"LIGNTNING IN AREA","COMPLETION_REMARKS":"wetr"}]}

which is valid JSON. The issue is I can't seem to get the "SUM(A.NUM_CUST), SUM(A.OUT_MINUTES), and MAX(A.RESTORE_DTS) to load into my model. I think it may be an issue with the parenthesis in the JSON.

The JSON is valid, here's my model and store info:



//DEFINE MODEL Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{ name: 'ID' },
{ name: 'XFMR' },
{ name: 'XPERS' },
{ name: 'EVENTNUM' },
{ name: 'SUM(A.NUM_CUST)' },
{ name: 'SUM(A.OUT_MINUTES)' },
{ name: 'MAX(A.RESTORE_DTS)', type: 'date', dateFormat: 'YmdHisT' },
{ name: 'OFF_DTS', type: 'date', dateFormat: 'YmdHisT' },
{ name: 'WE_PREMISE' },
{ name: 'WEATHER_CODE' },
{ name: 'COMPLETION_REMARKS' },
{ name: 'CAUSE' },
{ name: 'FEEDER' },
{ name: 'DEV_NAME' }

]
});
//GET DATA
var myStore = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'ajax',
url:'/omsportal/Application/Framework/getListByDevice.aspx?device=' + device,
reader:
{
root: 'Events',
totalProperty: 'totalCount'
}
},
autoLoad: true
});
//LOAD DATA
myStore.load();
// DRAW GRID
var grid = new Ext.grid.GridPanel({
store: myStore,
columns: [
//{ID: 'ID', header: 'ID', width: 50, sortable:true, dataIndex: 'ID'},
{ header: 'OUTAGE DEVICE', width: 100, sortable: true, dataIndex: 'DEV_NAME', renderer: deviceRenderer },
{ header: 'OFFTIME', width: 150, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y H:i:s'), dataIndex: 'OFF_DTS' },
{ header: 'RESTORE TIME', width: 150, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y H:i:s'), dataIndex: 'MAX(A.RESTORE_DTS)' },
{ header: 'CAUSE CODE', width: 150, sortable: true, dataIndex: 'CAUSE', renderer: causeDecode },
{ header: 'WEATHER CODE', width: 90, sortable: true, dataIndex: 'WEATHER_CODE' },
{ header: 'COMPLETION REMARKS', width: 200, sortable: true, dataIndex: 'COMPLETION_REMARKS' },
{ header: 'MINUTES', width: 60, sortable: true, dataIndex: 'SUM(A.OUT_MINUTES)' },
{ header: 'CUSTOMERS', width: 80, sortable: true, dataIndex: 'SUM(A.NUM_CUST)' },
{ header: 'CLOSED BY', width: 90, sortable: true, dataIndex: 'XPERS' },
{ header: 'EVENT NUMBER', width: 90, sortable: true, dataIndex: 'EVENTNUM', renderer: eventRenderer }
],
renderTo:'DeviceGrid'






});

Any help is appreciated. Thanks!

evant
12 Mar 2013, 3:10 PM
In this case you should probably give your field names something more simple and use a mapping to get the data:



Ext.define('User', {
extend : 'Ext.data.Model',
fields : [{
name : 'ID'
}, {
name : 'XFMR'
}, {
name : 'XPERS'
}, {
name : 'EVENTNUM'
}, {
name : 'foo',
mapping : 'SUM(A.NUM_CUST)'
}, {
name : 'bar',
mapping : 'SUM(A.OUT_MINUTES)'
}, {
name : 'baz',
mapping : 'MAX(A.RESTORE_DTS)',
type : 'date',
dateFormat : 'YmdHisT'
}, {
name : 'OFF_DTS',
type : 'date',
dateFormat : 'YmdHisT'
}, {
name : 'WE_PREMISE'
}, {
name : 'WEATHER_CODE'
}, {
name : 'COMPLETION_REMARKS'
}, {
name : 'CAUSE'
}, {
name : 'FEEDER'
}, {
name : 'DEV_NAME'
}]
});

Ext.onReady(function() {
var data = {
"totalCount" : "3",
"Events" : [{
"EVENTNUM" : "T00000000133",
"SUM(A.NUM_CUST)" : 61.0,
"SUM(A.OUT_MINUTES)" : 13.0,
"XPERS" : 796072.0,
"OFF_DTS" : "20130307084811CS",
"MAX(A.RESTORE_DTS)" : "20130307090200CS",
"CAUSE" : "10",
"FEEDER" : "17FL012011",
"DEV_NAME" : "T007F12127",
"WEATHER_CODE" : "THUNDERSTORM",
"COMPLETION_REMARKS" : "Testing Step Restore for Kasey"
}, {
"EVENTNUM" : "T00000000014",
"SUM(A.NUM_CUST)" : 61.0,
"SUM(A.OUT_MINUTES)" : 4.0,
"XPERS" : 796072.0,
"OFF_DTS" : "20130304135945CS",
"MAX(A.RESTORE_DTS)" : "20130304140400CS",
"CAUSE" : "09",
"FEEDER" : "17FL012011",
"DEV_NAME" : "T007F12127",
"WEATHER_CODE" : "NORMAL FOR SEASON",
"COMPLETION_REMARKS" : "COMPLETETION REMARKS FROM TRUCK ON TOFS. "
}, {
"EVENTNUM" : "T00000000173",
"SUM(A.NUM_CUST)" : 61.0,
"SUM(A.OUT_MINUTES)" : 3.0,
"XPERS" : 79607.0,
"OFF_DTS" : "20130307161424CS",
"MAX(A.RESTORE_DTS)" : "20130307161800CS",
"CAUSE" : "01",
"FEEDER" : "17FL012011",
"DEV_NAME" : "T007F12127",
"WEATHER_CODE" : "LIGNTNING IN AREA",
"COMPLETION_REMARKS" : "wetr"
}]
};

var myStore = Ext.create('Ext.data.Store', {
model : 'User',
proxy : {
type : 'memory',
reader : {
useSimpleAccessors: true,
type: 'json',
root : 'Events',
totalProperty : 'totalCount'
}
}
});

myStore.loadRawData(data);
console.log(myStore.first().get('foo'));

});

yesakmac
12 Mar 2013, 3:22 PM
Still isn't populating the store with the information, even after I modified the fields


{ name: 'ID' }, { name: 'XFMR' },
{ name: 'XPERS' },
{ name: 'EVENTNUM' },
{
name: 'NUM_CUST',
mapping: 'SUM(A.NUM_CUST)'},
{
name: 'OUT_MINUTES',
mapping: 'SUM(A.OUT_MINUTES)'},
{
name: 'RESTORE_DTS',
mapping: 'MAX(A.RESTORE_DTS)', type: 'date', dateFormat: 'YmdHisT'
},
{ name: 'OFF_DTS', type: 'date', dateFormat: 'YmdHisT' },
{ name: 'WE_PREMISE' },
{ name: 'WEATHER_CODE' },
{ name: 'COMPLETION_REMARKS' },
{ name: 'CAUSE' },
{ name: 'FEEDER' },
{ name: 'DEV_NAME' }

This advice is great, anything else you can think to look at?

evant
12 Mar 2013, 3:27 PM
I ran your code against the 4.2 beta, and it extracted the data as expected.

skirtle
12 Mar 2013, 5:56 PM
The key line in evant's code:


useSimpleAccessors: true

Blink and you'd miss it...