PDA

View Full Version : Accessing nested objects in JSON feed?



thirdboxcar
26 Apr 2011, 8:29 PM
I'll begin with the usual disclaimer: new to Sencha Touch/working with JSON, floundering in the dark. Any help or prodding in the right direction is appreciated more than you know!

I'm trying to get my app to fetch data from a public Google Spreadsheet JSON feed. From what I've managed to figure out, my current model is based on JSON arrays, NOT nested objects. How do I access and return a nested object?


Ext.regModel('Count', {
fields: [{name:'$t'}]

});



this.list = new Ext.List({
itemTpl: new Ext.XTemplate('<div>{$t}</div>'),
loadingText: false,
store: new Ext.data.Store({
model: 'Count',
proxy: {
type: 'scripttag',
url : 'http://spreadsheets.google.com/feeds/cells/0AuYDRk91MX8-dHhkV29ZVkRGNjlvZjV4QVBIVmJubVE/odb/public/basic?range=A1&alt=json',
reader: {
type: 'json',
root: 'feed'
}
}
})
});

The JSON data (extra stuff removed, above link will show all of it if need be, contains an email address I'd rather not post and have indexed):


{
"feed":{
"entry":[{
"content":{
"type":"text",
"$t":"11"
}
}]
}
}

If I plop in another JSON feed that uses arrays I can work with it just fine, but just can't figure out what I need to do to access that integer in the object that corresponds to $t. If I put "entry" as the root instead of "feed," I get an error that reads, "Uncaught TypeError: Cannot read property 'length' of undefined"

thirdboxcar
27 Apr 2011, 8:45 AM
So, it turns out that the list template does NOT like that $ in the variable that corresponds to the Google Spreadsheet JSON object. Here's my working solution:


Ext.regModel('Count', {
fields: [{name:'count', mapping:'content.$t'}]

});

this.list = new Ext.List({
itemTpl: new Ext.XTemplate('<div>{count}</div>'),
loadingText: false,
store: new Ext.data.Store({
model: 'Count',
proxy: {
type: 'scripttag',
url : 'http://spreadsheets.google.com/feeds/cells/0AuYDRk91MX8-dHhkV29ZVkRGNjlvZjV4QVBIVmJubVE/odb/public/basic?range=A1&alt=json',
reader: {
type: 'json',
root: 'feed.entry'
}
}
})
});