2012-10-10 35 views
1

我今天早上 剛纔問這個問題:「這是我的JSON文件:計數JSON線動態

[ 
{ 
    "Week": "1145", 
    "Sev_Logged": "3_major", 
    "From": "IN1" 
}, 
{ 
    "Week": "1145", 
    "Sev_Logged": "4_minor", 
    "From": "IN1" 
}, 
{ 
    "Week": "1145", 
    "Sev_Logged": "4_minor", 
    "From": "IN1" 
}, 
{ 
    "Week": "1145", 
    "Sev_Logged": "4_minor", 
    "From": "IN1" 
}, 
{ 
    "Week": "1145", 
    "Sev_Logged": "4_minor", 
    "From": "IN1" 
}, 
{ 
    "Week": "1145", 
    "Sev_Logged": "4_minor", 
    "From": "IN2" 
}, 
{ 
    "Week": "1145", 
    "Sev_Logged": "3_major", 
    "From": "IN2" 
}, 
]; 

我要算‘來自:IN1’字段爲每個‘星期’,每例如一週:1145我會得到:3,而對於「來源:IN2」我會得到2個

感謝」

謝謝VDP的回答,所以我這樣做: 我的店現在是這樣的:

Ext.define('Metrics.store.GraphData', { 
extend : 'Ext.data.Store', 
model : 'Metrics.model.GraphData', 
autoload : true, 

proxy : { 
    type : 'ajax', 
    url : 'data/metrics_data.json', 
    reader : { 
     type : 'json', 
     root : 'data', 
     successProperty : 'success' 
    } 
}, 
//data : GraphData, //povide inline data or load using proxy 
countBy: function(param){ 
    var count = {}; 
    this.each(function(item){ 
     var type = item.get(param); 
     if (Ext.isDefined(count[type])){ 
      count[type]++; 
     } else { 
      count[type] = 1; 
     }        
    }); 
    return count; 
} 
}); 

而且我的模型:

Ext.define('Metrics.model.GraphData', { 
extend: 'Ext.data.Model', 
//fields: ['week', 'sev'] 
fields : [ 
    {name: 'Week', type: 'int'}, 
    {name: 'Sev_Logged', type: 'string'}, 
    {name: 'From', type: 'string'} 
] 
}); 

由於我使用的ExtJS 4 MVC模式,我做了一個控制器至極控制按鈕的情況下,現在它看起來是這樣的:

launchGraph : function(button){ 
console.log('clicked the apply button'); 
var chartStore = this.getGraphDataStore(); 
chartStore.load({ 
    callback: this.onGraphDataLoad, 
    scope: this, 
    console.log(chartStore.countBy("From")) 
    }); 

但是,當我點擊申請按鈕我得到這個錯誤在我的控制器:

"Uncaught SyntaxError: Unexpected token . " 

而且這點' o線:

"console.log(chartStore.countBy("From"))" 

這似乎是我在我的商店引用錯誤。有任何想法嗎?

+0

你甚至不知道如何分析你的JSON字符串對象的JavaScript數組? –

+0

@ÁlvaroG.Vicario,我實際上使用extjs 4,但我不知道如何解析我的Json文件。我使用這些數據來繪製圖表,所以我需要對它們進行計數。 – salamey

+0

通過在chartStore.load之外放置「console.log(chartStore.countBy(」From「))」行來修復好問題。 – salamey

回答

0

如果你正在使用你提供,你可以只使用mimikomi的答案陣列或者其類似ExtJS的版本:

這裏是一個fiddle

function countBy(data, param) { 
    var count = {}; 
    Ext.each(data, function(item){ 
     var type = item[param]; 
     if (Ext.isDefined(count[type])) { 
      count[type]++; 
     } else { 
      count[type] = 1; 
     } 
    }); 
    return count; 
} 

如果從外部位置加載該json,最好將其加載到商店中。例如,您可以定義模型,商店併爲商店添加「特殊」功能。

Ext.define('Week', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     {name: 'Week', type: 'int'}, 
     {name: 'Sev_Logged', type: 'string'}, 
     {name: 'From', type: 'string'} 
    ] 
}); 

var store = Ext.create('Ext.data.Store', { 
    model: 'Week', 
    data : data, //povide inline data or load using proxy 
    countBy: function(param){ 
     var count = {}; 
     this.each(function(item){ 
      var type = item.get(param); 
      if (Ext.isDefined(count[type])){ 
       count[type]++; 
      } else { 
       count[type] = 1; 
      }        
     }); 
     return count; 
    } 
}); 

var countObject = store.countBy("From"); 

alert(Ext.encode(countObject)); 
//chrome developper tools, safari, opera can show us the contents of objects in a log too. IE, FF however only tell us it is an object. (pretty useless) 
console.log(countObject); 
//you can chose you're favorite notation to retreve the value (but I hope you knew this, it's pretty basic javascript) 
console.log(countObject.IN1); 
console.log(countObject['IN2']); 

這裏是一個fiddle

More info上裝載JSON動態到(使用代理)通過AJAX商店

+0

我已經做了你所說的一切,但控制檯返回:[Object object]。 – salamey

+0

當然我會返回一個對象:)使用chrome開發人員工具或'JSON.strigify(theobject)'或'Ext.encode(theobject)'將其轉換爲可讀的字符串我將更新小提琴 – VDP

+0

非常感謝,有用。 – salamey

1

如果var jsonData是從解析上面所示的JSON產生的陣列中,這會給你一個對象與每個取決於所使用的參數的計數..

function CountBy(parameter) { 
    var count = {}; 
    for (var i = 0; i < jsonData.length; i++) { 
    var type = jsonData[i][parameter]; 
    if (count[type] === undefined) { 
     count[type] = 1; 
    } else { 
     count[type]++; 
    } 
    } 
    return count; 
} 

結果:

CountBy("From") => {"IN1" : 5, "IN2" : 2} 
CountBy("Week") => {"1145" : 7} 
CountBy("Sev_Logged") => {"3_major": 2, "4_minor": 5} 
+0

只是澄清:如果它是JSON,它是一個字符串,而不是一個數組。 –

+0

那麼,首先調用字符串JSON.parse來獲取數據:'var jsonData = JSON.parse('[.........]');' – minikomi

+0

謝謝你的回答,但我使用extjs(對不起,我沒有提到它的問題),有沒有辦法循環我的商店,而不必將我的json文件轉換爲javascript數組? – salamey