PDA

View Full Version : JSON Reader / Datastore



Bleak
3 May 2010, 6:05 AM
I have configured a datastore as follows;



createGroupDataStore: function() {
if (this.aGroupStore == null) {

this.aGroupRecord = Ext.data.Record.create([
{name: 'id'},
{name: 'code'},
{name: 'name'},
{name: 'description'}
]);

this.aGroupReader = new Ext.data.JsonReader(
{
root: 'groups',
idProperty: 'id',
totalProperty:'results',
successProperty: 'success'
}, this.aGroupRecord);


this.aGroupStore = new Ext.data.Store(
{
autoLoad: true,
autoDestroy: true,
url: 'pp.do',
storeId: 'groupStore',
reader: this.aGroupReader,
baseParams: {ACTION:'getGroups'},
listeners: {
exception: function() {
console.log(arguments);
}
}
});

this.aGroupStore.load();


}
}
The JSON Data is as follows;



{
"responses": {
"response": {
"metadata": {
"request": "SUCCESS",
"requestor_id": {
"id": "1"
}
},
"groups": [
{
"id": "1",
"code": "ADMIN",
"name": "Adminisrator",
"description": "Administer the System"
},
{
"id": "2",
"code": "SPROJ",
"name": "SR. Project Manager",
"description": "Senior Project Manager"
},
{
"id": "3",
"code": "JPROJ",
"name": "JR. Project Manager",
"description": "Junior Project Manager"
},
{
"id": "4",
"code": "SPONS",
"name": "Sponsor",
"description": "Project Sponsor"
}
],
"success": true,
"results": 4
}
}
}
When I load this store, I get errors as follows;



[Object api=Object url=pp.do events=Object conn=Object, "response", "read", Object request=Object reader=Object scope=Object, Object tId=1 status=200 statusText=OK, TypeError: Record is undefined message=Record is undefined]
I just don't see what the problem is?

Thanks,
Dean.

Bleak
3 May 2010, 6:28 AM
I have discovered that the back-end needs to return a proper JSON Object, which I now do using the JSONObject (from www.json.org/java/ (http://www.json.org/java)). Now, in Firebug, I can see a JSON tab when the response is received, and it is properly reading the json object.

I still get an error, root is undefined. I am returning 4 records, and I get it 4 times.

Bleak
3 May 2010, 6:41 AM
Ok, my problem seems to have been the fact that the "groups" array which was what my root was configured to was nested, so I reformatted the JSON object to return as follows;



{
"results": 4,
"success": true,
"groups": [
{
"id": "1",
"description": "Administer the System",
"name": "Adminisrator",
"code": "ADMIN"
},
{
"id": "2",
"description": "Senior Project Manager",
"name": "SR. Project Manager",
"code": "SPROJ"
},
{
"id": "3",
"description": "Junior Project Manager",
"name": "JR. Project Manager",
"code": "JPROJ"
},
{
"id": "4",
"description": "Project Sponsor",
"name": "Sponsor",
"code": "SPONS"
}
],
"metadata": {
"request": "SUCCESS",
"requestor_id": {
"id": "1"
}
}
}
Once the groups array was moved, everything worked as expected.

Ok, in summary;

1. Ensure the response from the back-end is returning a proper JSON Object

To do this, I used the JSONObject's from www.json.org/java (http://www.json.org/java). I also set the response type to text/x-json. The source I use is from my action servlet, as the res is the response object.



res.setContentType("text/x-json; charset=UTF-8");
res.setHeader("Cache-Control", "no-chace");

JSONObject json = new JSONObject(UserManagerActions.getGroups(req));

json.write(res.getWriter());
2. Ensure that the root is not a nested object (or... figure out how to reference a nested object), my previous JSON object is in the earlier post, my new JSON Object is the one above.

Well, got the load working... off to the rest of CRUD ;)

Dean.

evant
3 May 2010, 6:50 AM
You can also use an expression, so for example:



root: 'foo.groups'


Where your data is



{
foo: {
groups: [{}, {}, {}]
}
}

Bleak
3 May 2010, 7:19 AM
Thanks evant, should have tried that ;)