0
我對從一種陣列形式到另一種形式的數據結構轉換有疑問。從一種陣列形式到另一種形式的數據轉換
我輸入數據的形式爲,
var testVar=[
{
"count": 1,
"term": "Company",
"Company": [
{
"Tata": [
{
"count": 1,
"term": "Tatagroup"
}
],
"sector": "Automotive",
"type": "Car"
},
]
},
{
"count": 2,
"term": "Country",
"Country": [
{
"France": [
{
"count": 1,
"term": "France"
}
],
"region": "Europe",
"Player": "Zidane",
"term": "France",
"code": "FRA"
},
{
"region": "Europe",
"Player": "Federer",
"term": "Switzerland",
"Switzerland": [
{
"count": 1,
"term": "Switzerland"
}
],
"code": "SWI"
}
]
}];
,我試圖把它轉化爲形式,
[ "Tata" : [{"sector" : "automotive"}, "type" : "car"], "France": [{"region" : "Europe}, {"Player" : "Zidane"} , {"code" : "FRA"}], "switzerland" : [{"region" : "Europe}, {"Player" : "Federer"} , {"code" : "SWI"}]];
我看起來想出像http://jsfiddle.net/X2apw/2/的代碼,BT的NT準確..
你的期望的結果是不JSON – Musa 2012-08-03 05:59:02
肯定的,它的一個數組 – 2012-08-03 06:01:38
第一:它的分解 - '[ 「塔塔」:[]]'將給你一個錯誤。 '{「塔塔」:[]}'是可以接受的。正如'[{「塔塔」:[]}]'。你現在所擁有的是一大堆破碎的JSON。 '[「type」:「car」]'也一樣。再次,'{「type」:「car」}'或'[{「type」:「car」}]''。第二......你爲什麼要這麼做?即使你是對的,你爲什麼要選擇像'parsedData [0] .Tata [1] .type; parsedData [1]。法國[0] .region; parsedData [1]。法國[1] .Player; parsedData [2] .switzerland [2]爲.code;'?不知道哪個對象在哪個數組中哪個對象處於哪一點的好處在哪裏? – Norguard 2012-08-03 06:08:28