2012-06-22 39 views
-2

可能重複存儲JSON文件:
Sencha Touch JSON Format煎茶觸摸 - 包含數組

我有以下的JSON文件,我想分析到我的煎茶觸摸應用。我無法弄清楚使用什麼類型的「存儲」(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]]}] 

任何幫助將不勝感激!

+0

即使您仍然迴應,請避免要求同樣的事情。 http://stackoverflow.com/questions/11110380/sencha-touch-json-format – hekomobile

回答

6

以下代碼適用於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(); 
+0

謝謝!如何將上述「目標」整合到此解決方案中?假設每個數據點都有一個目標。 – user1467584

+0

「目標」,意思是JSON文件中的「目標」鍵 – user1467584

+0

@ user1467584但在你的例子中,所有'datapoints'只有一個'target'。 –