2014-03-06 168 views
1

我有一個php multidimensional數組,我將它轉換成JSON與php json_encode函數。從php解析json到javascript

現在,編碼後,我回聲的JSON像echo'var javascriptJson ='。$ encoded_in_json_array;

我怎樣才能得到javascriptJson使用JavaScript的值?或者,我怎麼能將我的multidim php數組轉換爲JavaScript,以便我可以遍歷它或獲取值?

javascriptJson看起來像這樣:

var javascriptJson={ 
    "AMERICA CENTRALA SI DE SUD": { 
    "Argentina": { 
     "Recomandari pentru toti calatorii: Vaccinari de rutina": { 
     "Difterie\/Tetanos\/Pertussis": "background-color: b6c86;", 
     "Rujeola\/Rubeola\/Oreion": "background-color: b1ffff;", 
     "Varicela": "background-color: ffffff;", 
     "Gripa sezoniera": "background-color: ffffff;" 
     }, 
     "Recomandari de vaccinare pentru majoritatea calatorilor": { 
     "Hepatita A": "background-color: a6a6a6;", 
     "Tifos": "background-color: d9d9d9;" 
     }, 
     "Recomandari de vaccinare limitate (in functie de expunere)": { 
     "Hepatita B": "background-color: 7f7f7f;", 
     "Rabia": "background-color: 93d;" 
     }, 
     "Recomandari speciale de preventie": { 
     "Malaria": "background-color: 9bbb59;" 
     } 
    }, 
    "Belize": { 
     "Recomandari pentru toti calatorii: Vaccinari de rutina": { 
     "Difterie\/Tetanos\/Pertussis": "background-color: b6c86;", 
     "Rujeola\/Rubeola\/Oreion": "background-color: b1ffff;", 
     "Varicela": "background-color: ffffff;", 
     "Gripa sezoniera": "background-color: ffffff;" 
     }, 
     "Recomandari de vaccinare pentru majoritatea calatorilor": { 
     "Hepatita A": "background-color: a6a6a6;", 
     "Tifos": "background-color: d9d9d9;" 
     }, 
     "Recomandari de vaccinare limitate (in functie de expunere)": { 
     "Hepatita B": "background-color: 7f7f7f;", 
     "Rabia": "background-color: 93d;" 
     }, 
     "Recomandari speciale de preventie": { 
     "Malaria": "background-color: 9bbb59;" 
     } 
    }, 
    "Bolivia": { 
     "Recomandari pentru toti calatorii: Vaccinari de rutina": { 
     "Difterie\/Tetanos\/Pertussis": "background-color: b6c86;", 
     "Rujeola\/Rubeola\/Oreion": "background-color: b1ffff;", 
     "Varicela": "background-color: ffffff;", 
     "Gripa sezoniera": "background-color: ffffff;" 
     }, 
     "Recomandari de vaccinare pentru majoritatea calatorilor": { 
     "Hepatita A": "background-color: a6a6a6;", 
     "Tifos": "background-color: d9d9d9;" 
     }, 
     "Recomandari de vaccinare limitate (in functie de expunere)": { 
     "Hepatita B": "background-color: 7f7f7f;", 
     "Rabia": "background-color: 93d;" 
     }, 
     "Recomandari speciale de preventie": { 
     "Malaria": "background-color: 9bbb59;" 
     } 
    }, .....etc...etc 
+0

也許這可以幫助您的問題: http://stackoverflow.com/問題/ 13994858/javascript-convert-php-json-into-a-javascript-array – csanonymus

+0

通過使用javascriptJson [「美國中部城市」] [「伯利茲」] [「Recomandari speciale de preventi E 「] [」 瘧疾「]'? – h2ooooooo

+0

@ h2ooooooo他需要一個自動的方式來做到這一點... – csanonymus

回答

1
for (var i in javascriptJson) { 
    console.log(javascriptJson[i]); 
    for (var j in javascriptJson[i]) { 
    console.log(javascriptJson[i]); 
    console.log(javascriptJson[i][j]); 
    } 
} 
+0

遞歸應該更好地完成這項工作。 – WoIIe

1

如果要循環對象,你可以做

foreach(prop in obj){ 
console.log(prop); // would get you the property name 
console.log(obj[prop]); // would get you the value of that property 
} 

,可以讓你在這種情況下啓動。

+0

第二個控制檯語句是值的一個示例,這會在某些情況下返回下一個對象 – danblundell

1

從外觀的東西你應該有一個JavaScript對象後var javascriptJson = '.$encoded_in_json_array;行可用。

您可以通過使用這樣的對象迭代:

for(region in javascriptJson){ 
    var region_object = javascriptJson[region]; 
    // Code to handle each region 
    // If you want to look at each country in a region... 
    for(country in region_object){ 
     var country_object = region_object[country]; 
     // Code to handle each country object 
     // You can get values like: 
     // country_object['Recomandari pentru toti calatorii: Vaccinari de rutina'] 
    } 
} 
1

你的問題

我怎樣才能javascriptJson的使用JavaScript的價值?或者,我怎麼能將我的multidim php數組轉換爲JavaScript,以便我可以遍歷它或獲取值?

解決方案

var myObject = JSON.parse(/*json-string*/) 

現在,你可以通過這樣的數值迭代:

for (var property in myObject) { 
    // Do whatever you want 
}