来自 API 调用的 JSON 中缺少数据,给出错误
Missing data in JSON from API call, gives error
我向服务器发出 API 调用以获取数据
const api_url = "https://my_urlxxx"
addMyData()
.then(response => {
//console.log('Working!');
})
.catch(error => console.log(error.message));
async function addMyData() {
const response = await fetch(api_url);
const data = await response.json();
然后我将 JSON 变成 GeoJSON for Leaflet.js
geojson['type'] = 'FeatureCollection';
geojson ['features'] = [];
for (i = 0; i < data.length; i++) {
x = data[i].location.coordinate.longitude;
y = data[i].location.coordinate.latitude;
console.log('get ', data[i].itemId); //find the error...
console.log('get ', data[i].openingHours);
console.log('get ', data[i].openingHours.services[0]);
console.log('get ', data[i].openingHours.services[0].openDay); // error = undefined
var newFeature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [x,y]
},
"properties": {
"status": data[i].status,
"itemId": data[i].itemId,
"name": data[i].name,
"openingMonday": days(data[i].openingHours.services[0].openDay) + ' ' + num2time(data[i].openingHours.services[0].openTime) + '\u2014' + num2time(data[i].openingHours.services[0].closeTime)
}
}
geojson ['features'].push(newFeature);
}
//console.log(JSON.stringify(geojson));
loadData (geojson);
};
function loadData(MyGeoJSONData) {
MyLayer = L.geoJson(MyGeoJSONData, {
pointToLayer: function (feature, latlng) {
if (feature.properties.status != "1") {
return new L.shapeMarker(latlng, {
radius: 4,
color: '#252525', //#d53e4f
fillOpacity: 0.3,
weight: 2,
shape: 'circle'
})
}
},
问题是每个项目都没有“openingHours”的值。
这使得错误“未定义”并且我无法检索数据以显示在 Leaflet 地图上。
在创建 GeoJSON?
之前,我怎样才能传递或忽略这些值或者过滤掉它们
这是 openingHours JSON 文件的一部分,JSON 列表中的某些项目可能缺少整个部分,我无法通过此...
"openingHours": {
"services": [
{
"closeDay": "Monday",
"closeTime": "2359",
"openDay": "Monday",
"openTime": "0000"
},
{
"closeDay": "Tuesday",
"closeTime": "2359",
"openDay": "Tuesday",
"openTime": "0000"
},
{
"closeDay": "Wednesday",
"closeTime": "2359",
"openDay": "Wednesday",
"openTime": "0000"
},
{
"closeDay": "Thursday",
"closeTime": "2359",
"openDay": "Thursday",
"openTime": "0000"
},
{
"closeDay": "Friday",
"closeTime": "2359",
"openDay": "Friday",
"openTime": "0000"
},
{
"closeDay": "Saturday",
"closeTime": "2359",
"openDay": "Saturday",
"openTime": "0000"
},
{
"closeDay": "Sunday",
"closeTime": "2359",
"openDay": "Sunday",
"openTime": "0000"
}
],
"specialDates": [
]
},
你不需要在这里过滤,你可以只检查三元组和return一个空字符串(或者你可能需要的任何东西),如果它不存在:
"openingMonday": data[i]?.openingHours? days(data[i].openingHours.services[0].openDay) + ' ' + num2time(data[i].openingHours.services[0].openTime) + '\u2014' + num2time(data[i].openingHours.services[0].closeTime): ''
我向服务器发出 API 调用以获取数据
const api_url = "https://my_urlxxx"
addMyData()
.then(response => {
//console.log('Working!');
})
.catch(error => console.log(error.message));
async function addMyData() {
const response = await fetch(api_url);
const data = await response.json();
然后我将 JSON 变成 GeoJSON for Leaflet.js
geojson['type'] = 'FeatureCollection';
geojson ['features'] = [];
for (i = 0; i < data.length; i++) {
x = data[i].location.coordinate.longitude;
y = data[i].location.coordinate.latitude;
console.log('get ', data[i].itemId); //find the error...
console.log('get ', data[i].openingHours);
console.log('get ', data[i].openingHours.services[0]);
console.log('get ', data[i].openingHours.services[0].openDay); // error = undefined
var newFeature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [x,y]
},
"properties": {
"status": data[i].status,
"itemId": data[i].itemId,
"name": data[i].name,
"openingMonday": days(data[i].openingHours.services[0].openDay) + ' ' + num2time(data[i].openingHours.services[0].openTime) + '\u2014' + num2time(data[i].openingHours.services[0].closeTime)
}
}
geojson ['features'].push(newFeature);
}
//console.log(JSON.stringify(geojson));
loadData (geojson);
};
function loadData(MyGeoJSONData) {
MyLayer = L.geoJson(MyGeoJSONData, {
pointToLayer: function (feature, latlng) {
if (feature.properties.status != "1") {
return new L.shapeMarker(latlng, {
radius: 4,
color: '#252525', //#d53e4f
fillOpacity: 0.3,
weight: 2,
shape: 'circle'
})
}
},
问题是每个项目都没有“openingHours”的值。 这使得错误“未定义”并且我无法检索数据以显示在 Leaflet 地图上。 在创建 GeoJSON?
之前,我怎样才能传递或忽略这些值或者过滤掉它们这是 openingHours JSON 文件的一部分,JSON 列表中的某些项目可能缺少整个部分,我无法通过此...
"openingHours": {
"services": [
{
"closeDay": "Monday",
"closeTime": "2359",
"openDay": "Monday",
"openTime": "0000"
},
{
"closeDay": "Tuesday",
"closeTime": "2359",
"openDay": "Tuesday",
"openTime": "0000"
},
{
"closeDay": "Wednesday",
"closeTime": "2359",
"openDay": "Wednesday",
"openTime": "0000"
},
{
"closeDay": "Thursday",
"closeTime": "2359",
"openDay": "Thursday",
"openTime": "0000"
},
{
"closeDay": "Friday",
"closeTime": "2359",
"openDay": "Friday",
"openTime": "0000"
},
{
"closeDay": "Saturday",
"closeTime": "2359",
"openDay": "Saturday",
"openTime": "0000"
},
{
"closeDay": "Sunday",
"closeTime": "2359",
"openDay": "Sunday",
"openTime": "0000"
}
],
"specialDates": [
]
},
你不需要在这里过滤,你可以只检查三元组和return一个空字符串(或者你可能需要的任何东西),如果它不存在:
"openingMonday": data[i]?.openingHours? days(data[i].openingHours.services[0].openDay) + ' ' + num2time(data[i].openingHours.services[0].openTime) + '\u2014' + num2time(data[i].openingHours.services[0].closeTime): ''