我有以下的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对象循环重建......
喜新,我想你回答它的工作...你能告诉我你为什么用索引1的el.geography [1] ......只是想知道...... – troy 2013-04-21 08:58:07
地理[1]是国家('[“Europe” ,“德国”] [1] ===“德国”),它看起来足以检查两个地理数组是否相等 – neo 2013-04-21 09:05:36