2013-04-21 119 views
0

我有以下的JSON對象解析來自JSON obejct特定記錄

 var testObj = { 
     "CompanyA": [ 
      { "geography": [ "Europe", "Germany" ], "productLine": "Produce", "revenue": { "2022": 130143, "2021": 172122, "2020": 103716 } }, 
      { "geography": [ "Europe", "France" ], "productLine": "Clothing", "revenue": { "2022": 85693, "2021": 91790, "2020": 77650 } }, 
      { "geography": [ "Europe", "France" ], "productLine": "Electronics", "revenue": { "2022": 121987, "2021": 62435, "2020": 65834 } }, 
      { "geography": [ "Europe", "Germany" ], "productLine": "Produce", "revenue": { "2022": 130143, "2021": 107447, "2020": 145543 } }, 
      { "geography": [ "Europe", "Germany" ], "productLine": "Clothing", "revenue": { "2022": 77903, "2021": 97139, "2020": 110346 } }, 
      { "geography": [ "Europe", "Germany" ], "productLine": "Electronics", "revenue": { "2022": 110897, "2021": 155282, "2020": 128696 } }, 
      { "geography": [ "South America", "Brazil" ], "productLine": "Clothing", "revenue": { "2022": 66217, "2021": 55798, "2020": 66643 } }, 
      { "geography": [ "South America", "Brazil" ], "productLine": "Electronics", "revenue": { "2022": 94262, "2021": 100560, "2020": 56272 } } 
      ], 
      "CompanyB": [ 
      { "geography": [ "Europe", "United Kingdom" ], "productLine": "Produce", "revenue": { "2022": 281110, "2021": 242965, "2020": 221863 } }, 
      { "geography": [ "Europe", "United Kingdom" ], "productLine": "Clothing", "revenue": { "2022": 168270, "2021": 121161, "2020": 60919 } }, 
      { "geography": [ "Europe", "United Kingdom" ], "productLine": "Electronics", "revenue": { "2022": 239537, "2021": 131959, "2020": 97047 } }, 
      { "geography": [ "Europe", "Ireland" ], "productLine": "Produce", "revenue": { "2022": 74963, "2021": 43406, "2020": 54623 } }, 
      { "geography": [ "Europe", "Ireland" ], "productLine": "Clothing", "revenue": { "2022": 44872, "2021": 24797, "2020": 16010 } }, 
      { "geography": [ "Europe", "Ireland" ], "productLine": "Electronics", "revenue": { "2022": 63877, "2021": 94185, "2020": 87098 } } 
      ], 
     ......... 
     }; 

有三個下拉菜單在我的網頁像公司,地理學(CountryName2nd元素)和PRODUCTLINE ...

根據所選下拉值我需要拿起收入..

我能捕捉到公司名稱地理(國家名稱)PRODUCTLINE由用戶選擇的,但努力去特定的收入......

我能得到的收入是這樣

console.log(testObj[company][1].revenue); 

但在這裏,我不知道具體的記錄,因爲我需要選擇根據國家和PRODUCTLINE

我得到了這個解決方案。但我想知道什麼是解析JSON對象的最佳方式......它是根據我們的需要,或通過JSON對象循環重建......

回答

0

可以過濾匹配的對象這樣

var company = "CompanyA"; 
var country = "Germany"; 
var productLine = "Produce"; 

var res = testObj[company].filter(function(el){ 
    return el.geography[1] == country && productLine == el.productLine; 
}); 
console.log(res); 
+0

喜新,我想你回答它的工作...你能告訴我你爲什麼用索引1的el.geography [1] ......只是想知道...... – troy 2013-04-21 08:58:07

+1

地理[1]是國家('[「Europe」 ,「德國」] [1] ===「德國」),它看起來足以檢查兩個地理數組是否相等 – neo 2013-04-21 09:05:36

0

你只需要遍歷數組,直到你找到相匹配的一個:

var companyArr = testObj[company]; 
for(var i = 0; i < companyArr.length; i++){ 
    if(companyArr[i].geography[1] == country && companyArr[i].productLine == productLine) 
     break; 
} 

if(i < companyArr.length){ // Found a match 
    console.log(companyArr[i]); // Log the matching object 
} 

注意,如果重組您的JSON是一個選項,那麼你可以完全避免的循環,只是這樣做:

testObj[company][country][productLine].revenue 
+0

你好,謝謝你的回覆...我試過你的解決方案,它給了我一個錯誤「無法讀取屬性'1'未定義」 – troy 2013-04-21 08:46:17

+0

我認爲如果(companyArr.geography [1]應該是如果(companyArr.geography [i ] – Silvertiger 2013-04-21 08:48:09

+0

我想它,以及它告訴我 「無法讀取屬性 '0' 的未定義」 – troy 2013-04-21 08:53:25