我有2個外部json文件,我必須使用鍵和值從這些文件中讀取數據。我的應用程序不依賴於obj.name等請幫我打印這些值。打印鍵和json對象的值
Planets.json
{
"Planets": [
{ "name": "Mercury",
"color": "Orange",
"distance": "57.91 km",
"radius": "2340 km",
"year": "0.24085 Earth",
"day": "88 days",
"mass": "0.054 Earth"
},
{ "name": "second",
"color": "Orange",
"distance": "57.91 km",
"radius": "2340 km",
"year": "0.24085 Earth",
"day": "88 days",
"mass": "0.054 Earth"
}
}
Airports.json
{
"Airports": [
{
"listing": "East 34th Street Heliport",
"iata": "TSS",
"type": "Heliport",
"size": "Tiny"
}
}
,這是我努力的代碼。
$.ajax({
url:"planets.json",
dataType: "json",
success:function(json){
$.each(json, function(key, value){
console.log(key + ": " + value);
});
}
});
和我得到了這一點,把控制檯
行星:[目標對象]
如果我改變我的文件名行星到機場然後我必須修改所有的東西 –
如果你迭代你的頂級對象(json),你可以得到鑰匙:行星,機場等。 –
@SaqibSaqib:更新 –