Sencha Touch - 存储包含数组的 JSON 文件

Sencha Touch - Store JSON file containing Array

本文关键字:JSON 文件 数组 包含 Touch 存储 Sencha      更新时间:2023-09-26

可能的重复项:
煎茶触摸 JSON 格式

我有以下 JSON 文件,我想解析到我的 Sencha Touch 应用程序中。我无法弄清楚要使用哪种类型的"存储"(Store,JsonStore,ArrayStore等)以及"字段"设置会是什么样子。数组中的每个"数据点"都应存储为 xValue 和 yValue。我不明白如何在没有单独标签的情况下阅读这些要点......

[{"target": "stats.server1", "datapoints": [[22, 34], [99, 12], [13, 15], [56, 12], [34, 2], [13, 23], [23, 34], [55, 88]]},{"target": "stats.server2", "datapoints": [[22, 34], [99, 12], [13, 15], [56, 12], [34, 2], [13, 23], [23, 34], [55, 88]]}]

任何帮助将不胜感激!

以下代码适用于 Ext JS 4.1,但据我了解,Touch 2 中的数据框架是相同的。

// A reader defines how to process incoming data.
Ext.define('My.PointsReader', {
    extend: 'Ext.data.reader.Json',
    alias: 'reader.points',
    getData: function(data) {       // overriding
        data = this.callParent(arguments);
        var result = [];
        Ext.each(data, function(entry) {
            Ext.each(entry.datapoints || [], function(point) {
                result.push({
                    xValue: point[0], yValue: point[1],
                    target: entry.target
                });
            });
        });
        return result;
    }
});
// A store is always Ext.data.Store (or TreeStore, for trees).
// But its proxy, reader and writer can be customized.
var store = Ext.create('Ext.data.Store', {
        fields: ['xValue', 'yValue', 'target'],
        proxy: {
            type: 'ajax',
            url: 'test.json',
            reader: 'points'
        }
    });
store.load();