PDA

View Full Version : Modifying a jsonData response



cavalleydude
29 Jun 2010, 9:58 AM
I have some web services that return JSON content that does not work with Ext.data.Store or JSONStore objects. I cannot modify the web service, so I need to modify the returned jsonData and modify the content (key/values). How is this done exactly? It looks like it's done in the readRecords function, but I cannot find any clear examples... I only need a simple jsonData editing example. Any help would be appreciated.

mankz
29 Jun 2010, 9:59 AM
How does the server json response look?

Animal
29 Jun 2010, 10:02 AM
The docs for JsonReader are a little out of date.

root can be specified as a function which returns the root Array of rows. So that give you the opportunity to massage the data before JsonReader loops through it reading Records out of it.

cavalleydude
29 Jun 2010, 10:23 AM
Here is my Store and 'var data' is exactly how the data is formatted from the server. I want to show all the west-units, but cannot for the life of me figure out how, because the west-unit array is a child of main-units. I've tried setting the west-unit as 'root' but it does not work. So, I thought if I edited the JSON data and made west-units a simple array at level 1 (instead of nested), it would WILL work that way. The problem is I cannot change the web service, otherwise this would not be a problem.



this.store = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({
headers: {'Authorization':Ext.util.Cookies.get('auth'),
'Accept':'application/json'},
scope:this,
disableCaching:false,
url:'/billapp/rest/getunits',
method : 'GET'
}),
reader: new Ext.data.JsonReader({
root: 'west-unit',
id: 'code'
}, [
{name:'code'},
{name:'description'}
] ),

//
// ADD EVENT LISTENERS
//
listeners:{
beforeload:{
fn: function(store, options){
if (options.startPath && (options.startPath.length > 0)){
//store.baseParams.startPath = options.startPath;
}
},
scope:this
},
add: {
fn: function(store,records,index){
//console.log(records);
this.storeLoaded = true;
}
},
load: {
fn: function(store,records,options){
//console.log(records);
this.storeLoaded = true;
}
},
exception: {
fn: function(misc, misc){
//console.log(records);
this.storeLoaded = false;
}
},
loadexception: {
fn: function(misc, misc){
//console.log(records);
this.storeLoaded = false;
}
},
scope:this
},
sortInfo:{field: 'description', direction: "ASC"}
});

// This is formatted exactly as the server returns the data.
var data =
{"main-units":
{"west-unit":[ {"code":"3-E","description":"Thal 3-E"},
{"code":"3-NE","description":"3-NE"},
{"code":"3-NW","description":"3-NW"},
{"code":"3-SW","description":"3-SW"},
{"code":"8SIS","description":"8S-NSICU"},
{"code":"CLAB","description":"CIC"},
{"code":"GLB2","description":"GI-310"}]
}
};

this.store.loadData( data );

CrazyEnigma
29 Jun 2010, 10:43 AM
You can specify the root at any level.



root: "main-units.west-unit",

cavalleydude
29 Jun 2010, 10:48 AM
I tried that first, but it does not work.

CrazyEnigma
29 Jun 2010, 10:51 AM
It is possible it didn't like the "-". In this case, you may have to use:


data["main-units"]["west-unit"]


"-" is also an operator for subtract, so the eval might not like it.

cavalleydude
29 Jun 2010, 10:58 AM
Yes, I think it is the "-" hyphen, I tried your suggestion... so far it's not working. I'll keep trying, it's 1 step closer. Thanks for the help.

Animal
29 Jun 2010, 10:59 AM
Use



root: function(d) {
return d['main-units']['west-unit'];
}


As I already suggested.

cavalleydude
29 Jun 2010, 11:07 AM
Yes, that worked!! Thanks to all responders.

This issue has been bugging me for a few weeks ;), I really appreciate the assistance.

CrazyEnigma
29 Jun 2010, 11:59 AM
Wow, Animal. Your original response with the next one was so subtle that I learned something today. I'll have to keep that in mind if I run into that problem.

Animal
29 Jun 2010, 1:47 PM
I'll update the docs.

Later...

... in life.

arunkumargm1987
29 Jun 2010, 7:33 PM
so it means if we put '-' or '+' within single quotes they don't behave as arithmetic operators ?

CrazyEnigma
30 Jun 2010, 6:27 AM
@arunkumargm1987: no. That is incorrect.

You can access the object's property two ways:


var value = data.main-units.west-unit;
var value = data["main-units"]['west-unit'];

But the former would fail because the "-" is treated as an operator. To the compiler, it looks like data.main minus units.west minus unit, but that is incorrect.

However, when specifying this in Sencha.ExtJS, you are specifying this value as a string parameter in a config: "main-units.west-unit". This means that ExtJS is evaluating the string to interpret the location of the data, and would fail because it now is treated as an operator rather than a reference to data["main-units"]['west-unit']. Single quotes and double quotes are ambiguous in Javascript, with exception to specifying the template in an XTemplate.