2

我将从通常的免责声明开始:Sencha Touch 的新手/使用 JSON,在黑暗中挣扎。任何朝着正确方向的帮助或刺激都比你知道的更感激!

我正在尝试让我的应用程序从公共 Google 电子表格 JSON 提要中获取数据。据我所知,我当前的模型基于 JSON 数组,而不是嵌套对象。如何访问和返回嵌套对象?

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数据(删除了额外的内容,如果需要,上面的链接将显示所有内容,包含一个我不想发布并已编入索引的电子邮件地址):

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

如果我插入另一个使用数组的 JSON 提要,我可以很好地使用它,但只是无法弄清楚我需要做什么才能访问与 $t 对应的对象中的那个整数。如果我将“entry”而不是“feed”作为根,我会收到一条错误消息,内容为“Uncaught TypeError: Cannot read property 'length' of undefined”。

4

1 回答 1

4

解决方案!原来 Sencha 不喜欢我的模板变量中的 $。

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'
                    }
                }
            })
        });
于 2011-04-27T16:42:24.663 回答