2017-06-13 20 views
-4

JSON如何使用平板JSON數據

[ 
{"CountryID":1,"CountryName":"Austria","CountryG2":"Austria","CountryG3":"DACH","CountryG4":"Western Europe"}, 
{"CountryID":2,"CountryName":"Germany","CountryG2":"Germany","CountryG3":"DACH","CountryG4":"Western Europe"}, 
{"CountryID":3,"CountryName":"Ireland, United Kingdom","CountryG2":"UK&I","CountryG3":"UK&I","CountryG4":"Western Europe"}, 
{"CountryID":4,"CountryName":"Belarus","CountryG2":"Belarus","CountryG3":"ESE","CountryG4":"CEMA"}, 
{"CountryID":5,"CountryName":"Bosnia","CountryG2":"Bosnia","CountryG3":"ESE","CountryG4":"CEMA"}, 
{"CountryID":6,"CountryName":"Serbia","CountryG2":"Serbia","CountryG3":"ESE","CountryG4":"CEMA"}, 
{"CountryID":7,"CountryName":"Croatia","CountryG2":"Croatia","CountryG3":"ESE","CountryG4":"CEMA"}, 
{"CountryID":8,"CountryName":"Greece","CountryG2":"Greece","CountryG3":"ESE","CountryG4":"CEMA"}, 
{"CountryID":9,"CountryName":"Poland","CountryG2":"Poland","CountryG3":"Central Europe","CountryG4":"CEMA"}, 
{"CountryID":10,"CountryName":"Czech Republic","CountryG2":"Czech Republic","CountryG3":"Central Europe","CountryG4":"CEMA"}, 
{"CountryID":12,"CountryName":"Slovakia","CountryG2":"Slovakia","CountryG3":"Central Europe","CountryG4":"CEMA"}, 
{"CountryID":13,"CountryName":"Hungary","CountryG2":"Hungary","CountryG3":"Central Europe","CountryG4":"CEMA"}, 
{"CountryID":14,"CountryName":"Israel","CountryG2":"Israel","CountryG3":"Central Europe","CountryG4":"CEMA"}, 
{"CountryID":15,"CountryName":"Russia","CountryG2":"Russia","CountryG3":"Russia","CountryG4":"CEMA"} 
] 

集團通過 CountryG4> CountryG3> CountryG2>國家或地區名稱

+0

thisArg把你的JSON的代碼片段 –

+7

向我們展示你已經嘗試了什麼。 SO不是代碼訂購服務,您只需放下您的要求即可。 – CBroe

+0

所以,你需要一些js,php,jquery(js)......或者這是一種什麼樣的調查? – YvesLeBorg

回答

1

您可以使用嵌套散列表方法。

var data = [{ CountryID: 1, CountryName: "Austria", CountryG2: "Austria", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 2, CountryName: "Germany", CountryG2: "Germany", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 3, CountryName: "Ireland, United Kingdom", CountryG2: "UK&I", CountryG3: "UK&I", CountryG4: "Western Europe" }, { CountryID: 4, CountryName: "Belarus", CountryG2: "Belarus", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 5, CountryName: "Bosnia", CountryG2: "Bosnia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 6, CountryName: "Serbia", CountryG2: "Serbia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 7, CountryName: "Croatia", CountryG2: "Croatia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 8, CountryName: "Greece", CountryG2: "Greece", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 9, CountryName: "Poland", CountryG2: "Poland", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 10, CountryName: "Czech Republic", CountryG2: "Czech Republic", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 12, CountryName: "Slovakia", CountryG2: "Slovakia", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 13, CountryName: "Hungary", CountryG2: "Hungary", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 14, CountryName: "Israel", CountryG2: "Israel", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 15, CountryName: "Russia", CountryG2: "Russia", CountryG3: "Russia", CountryG4: "CEMA" }], 
 
    keys = ['CountryG4', 'CountryG3', 'CountryG2'], 
 
    result = []; 
 

 
data.forEach(function (a) { 
 
    keys.reduce(function (r, k) { 
 
     if (!r[a[k]]) { 
 
      r[a[k]] = { _: [] }; 
 
      r._.push({ name: a[k], children: r[a[k]]._ }); 
 
     } 
 
     return r[a[k]]; 
 
    }, this)._.push({ CountryID: a.CountryID, CountryName: a.CountryName }); 
 
}, { _: result }); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相同,但沒有使用的Array#forEach

var data = [{ CountryID: 1, CountryName: "Austria", CountryG2: "Austria", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 2, CountryName: "Germany", CountryG2: "Germany", CountryG3: "DACH", CountryG4: "Western Europe" }, { CountryID: 3, CountryName: "Ireland, United Kingdom", CountryG2: "UK&I", CountryG3: "UK&I", CountryG4: "Western Europe" }, { CountryID: 4, CountryName: "Belarus", CountryG2: "Belarus", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 5, CountryName: "Bosnia", CountryG2: "Bosnia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 6, CountryName: "Serbia", CountryG2: "Serbia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 7, CountryName: "Croatia", CountryG2: "Croatia", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 8, CountryName: "Greece", CountryG2: "Greece", CountryG3: "ESE", CountryG4: "CEMA" }, { CountryID: 9, CountryName: "Poland", CountryG2: "Poland", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 10, CountryName: "Czech Republic", CountryG2: "Czech Republic", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 12, CountryName: "Slovakia", CountryG2: "Slovakia", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 13, CountryName: "Hungary", CountryG2: "Hungary", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 14, CountryName: "Israel", CountryG2: "Israel", CountryG3: "Central Europe", CountryG4: "CEMA" }, { CountryID: 15, CountryName: "Russia", CountryG2: "Russia", CountryG3: "Russia", CountryG4: "CEMA" }], 
 
    keys = ['CountryG4', 'CountryG3', 'CountryG2'], 
 
    result = []; 
 

 
data.forEach(function (hash) { 
 
    return function (a) { 
 
     keys.reduce(function (r, k) { 
 
      if (!r[a[k]]) { 
 
       r[a[k]] = { _: [] }; 
 
       r._.push({ name: a[k], children: r[a[k]]._ }); 
 
      } 
 
      return r[a[k]]; 
 
     }, hash)._.push({ CountryID: a.CountryID, CountryName: a.CountryName }); 
 
    }; 
 
}({ _: result })); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

瘋了!你能讓我知道,如果我正確地理解這一點:1)迭代值2)減少鍵陣列。 3)將ForEach的執行上下文設置爲對象鍵「_」值「result」空數組。 4)。利用它的「this」推入forEach內部的空數組。 5)將包含國名的對象連同其父國countryId一起推入「結果」中。 5)然後你檢查是否有一個數組的國家名稱被你的鍵數組散列6)如果沒有這個哈希名稱的數組文字,你爲它分配一個對象鍵屬性爲空的數組值,屬性? – Rick

+0

這個函數可以用來創建列表嗎? –

+1

@arrow,對,你理解正確。如果你喜歡的話,你可以用一個封閉值來代替這個''這個''。 –

0

您可以使用JSON.parse()來函數用於創建此嵌套列表。 假設你在變量中有這個json,數據。 你可以像

var parsedData = JSON.parse(data); 

某事,這會給你所有需要的嵌套漂亮的一個很好的JavaScript對象。

如果你有這樣的JSON保存在一個文件上的node.js,你可以這樣做

var parsedData = JSON.parse(fs.readFileSync("filename.json")); 
+0

OP未指定是否要求提供js或php解決方案。不要回答這樣的壞問題。 – YvesLeBorg

+0

我認爲這是來自標籤的JavaScript。這裏有一個完整的新手用stackoverflow。 –

0

嘗試下一個(如果它的PHP相關的):如果它是一個

$data = [ 
    ["CountryID" => 1, ... // put your array here 
]; 
// or use: $data = json_decode($jsonString, true); 

$store = array(); 
foreach ($data as $val) { 
    $store[$val['CountryG4']][$val['CountryG3']][$val['CountryG2']] = $val['CountryName']; 
} 

var_dump($store); 

JavaScript問題:

var store = {}; 
json.forEach(function (data) { 
    store[data.CountryG4][data.CountryG3][data.CountryG2] = data.CountryName; 
}); 

console.log(store);