我发送api调用并以json格式检索数据.
$.getJSON(weatherAPI, function(data){
// get data
});
Run Code Online (Sandbox Code Playgroud)
如果我调用该对象data及其中一个属性(data.weather),我得到以下输出
[Object {
description: "clear sky",
icon: "xyz",
main: "clear"
}]
Run Code Online (Sandbox Code Playgroud)
我似乎无法使用data.weather.description"晴空"获得所需的输出
整个json格式数据如下
天气是一个array of Objects,所以你需要指定index和访问属性
console.log(data.weather[0].description);
Run Code Online (Sandbox Code Playgroud)
如果您需要打印所有元素的值,请使用.foreach或 .map()
.map()返回一个新数组而不返回.forEach().forEach()只对数组中的每个值进行操作.如果您只需要控制输出值使用forEach.
使用forEach,
data.weather.forEach((e) => {
console.log(e.description);
});
Run Code Online (Sandbox Code Playgroud)
使用.map
data.weather.map((e) => {
console.log(e.description);
return e;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |