【发布时间】:2022-01-12 20:24:40
【问题描述】:
我希望我的代码获取 JSON 并读取伦敦的温度值。
任何帮助将不胜感激。
但是我有两个问题,获取的数据显示:[object Object]
- 如何修复?
- 如何让它获取伦敦的温度值,即 main 和 temp。
可能是这样的:
data.["main"].temp
伦敦的开尔文温度是 277.21。
我的 JSON:
{
"coord": { "lon": -0.1257, "lat": 51.5085 },
"weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" }],
"base": "stations",
"main": { "temp": 277.21, "feels_like": 275.36, "temp_min": 274.4, "temp_max": 279.13, "pressure": 1042, "humidity": 93 },
"visibility": 9000,
"wind": { "speed": 2.06, "deg": 230 },
"clouds": { "all": 9 },
"dt": 1642014889,
"sys": { "type": 2, "id": 2019646, "country": "GB", "sunrise": 1641974505, "sunset": 1642004119 },
"timezone": 0,
"id": 2643743,
"name": "London",
"cod": 200
}
我的错误代码:
$.ajax({
url: "https://api.openweathermap.org/data/2.5/weather?q=London&appid=a7a01852c805157a4c8334e87e39c75c",
cache: false,
dataType: "json",
complete: function(data) {
tempLondon = data;
$('#tempLondon').append('The temperature is ' + tempLondon );
console.log(tempLondon);
}
});
【问题讨论】:
-
不要混合点和括号表示法(它不能那样工作)。
data["main"].temp
标签: javascript json ajax