访问JSON feed中的嵌套对象-Sencha Touch [英] Accessing nested objects in JSON feed - Sencha Touch
问题描述
我将以通常的免责声明开始:Sencha Touch的新功能/使用JSON,在黑暗中挣扎.正确方向上的任何帮助或建议都比您所了解的更多!
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!
我正在尝试让我的应用从公开的Google Spreadsheet JSON Feed中获取数据.据我设法弄清楚,我当前的模型基于JSON数组,而不是嵌套对象.如何访问和返回嵌套对象?
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'
}
}
})
});
JSON数据(已删除多余的东西,上面的链接将在需要时显示所有信息,其中包含我不希望发布并已建立索引的电子邮件地址):
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"
}
}]
}
}
如果我放入另一个使用数组的JSON提要中,则可以使用它,但不能弄清楚我需要做什么来访问对应于$ t的对象中的整数.如果我将"entry"作为根而不是"feed",则会收到一条错误消息:"Uncaught TypeError:无法读取未定义的属性'length'."
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."
推荐答案
解决方案!原来Sencha不喜欢模板变量中的$.
The solution! Turns out Sencha didn't like the $ in my template variable.
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'
}
}
})
});
这篇关于访问JSON feed中的嵌套对象-Sencha Touch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!