Javascript 难以访问api'中的信息;s返回json数组。我做错了什么?
我试图从weather.gov api()的json响应中获取信息。我很有可能过度思考了我的问题,因为我得到了预期数量的响应,但所有2748个响应都是“未定义的” 我尝试过通过点表示法访问json对象响应中看到的属性,我尝试过.map和.forEach方法来获得不同的输出,我读过几十篇博客文章,这些文章似乎涉及“更简单”的api。我在这里也读过几篇文章,但我不明白到底是怎么回事 在这一点上我很确定我的错误很小,但我看不出来Javascript 难以访问api'中的信息;s返回json数组。我做错了什么?,javascript,arrays,api,Javascript,Arrays,Api,我试图从weather.gov api()的json响应中获取信息。我很有可能过度思考了我的问题,因为我得到了预期数量的响应,但所有2748个响应都是“未定义的” 我尝试过通过点表示法访问json对象响应中看到的属性,我尝试过.map和.forEach方法来获得不同的输出,我读过几十篇博客文章,这些文章似乎涉及“更简单”的api。我在这里也读过几篇文章,但我不明白到底是怎么回事 在这一点上我很确定我的错误很小,但我看不出来 const endpoint = 'https://api.weath
const endpoint = 'https://api.weather.gov/stations/';
async function getStations() {
const response = await fetch(endpoint);
const data = await response.json();
stationInfo = data.observationStations;
// data.observationStations.forEach(function(e) {
// console.log(e.properties);
// });
for(let i in stationInfo) {
// ext = stationInfo[i].slice(-4);
// console.log(endpoint + ext);
console.log(stationInfo[i].id)
}
}
getStations();
下面是我正在尝试使用的json示例
{
"@context": [
"https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld",
{
"wx": "https://api.weather.gov/ontology#",
"s": "https://schema.org/",
"geo": "http://www.opengis.net/ont/geosparql#",
"unit": "http://codes.wmo.int/common/unit/",
"@vocab": "https://api.weather.gov/ontology#",
"geometry": {
"@id": "s:GeoCoordinates",
"@type": "geo:wktLiteral"
},
"city": "s:addressLocality",
"state": "s:addressRegion",
"distance": {
"@id": "s:Distance",
"@type": "s:QuantitativeValue"
},
"bearing": {
"@type": "s:QuantitativeValue"
},
"value": {
"@id": "s:value"
},
"unitCode": {
"@id": "s:unitCode",
"@type": "@id"
},
"forecastOffice": {
"@type": "@id"
},
"forecastGridData": {
"@type": "@id"
},
"publicZone": {
"@type": "@id"
},
"county": {
"@type": "@id"
}
}
],
"id": "https://api.weather.gov/stations/KVBS",
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-93.629999999999995,
29.48
]
},
"properties": {
"@id": "https://api.weather.gov/stations/KVBS",
"@type": "wx:ObservationStation",
"elevation": {
"value": 68.275199999999998,
"unitCode": "unit:m"
},
"stationIdentifier": "KVBS",
"name": "SABINE 13B OIL PLATFORM",
"timeZone": "America/Chicago",
"forecast": "https://api.weather.gov/zones/forecast/GMZ450"
}
}
因为我成功地获得了json数组中每个项目的url,所以我希望能够访问这些项目的属性——相反,我得到的是“未定义”。然而,正如我前面所说,我得到了“未定义”的预期次数。关键是
观测站
属性对象从https://api.weather.gov/stations/
是字符串数组,而不是对象数组
因此,要打印字符串,应执行以下操作:
console.log(stationInfo[i])
而不是:
console.log(stationInfo[i].id)
希望对您有所帮助,您可以尝试以下方法:
const端点=”https://api.weather.gov/stations/';
const getStations=async()=>{
const response=等待获取(端点);
const data=wait response.json();
data.observationStations.forEach(item=>console.log(item));
}
getStations()代码>循环观察站您只需要一个简单的forEach
const端点=”https://api.weather.gov/stations/';
const getStations=async()=>{
const response=等待获取(端点);
const data=wait response.json();
data.observationStations.forEach(station=>console.log(station))
}
getStations()代码>如果要访问观测站
:
const端点=”https://api.weather.gov/stations/';
异步函数getStations(){
console.log('calling');
const data=wait fetch(endpoint).then(res=>res.json());
const observationStations=数据。观测站;
forEach(observationStation=>console.log(observationStation));
}
getStations()代码>这是我可以在console.log(数据)中看到的数组。forEach是否更高效?您首先需要通过HTTP服务发出GET请求。@Michael he是。这就是让doesOh geez的原因。是的,我现在在做生意。非常感谢你的帮助!