我已经尝试了一切我能想到的来测试这个json数组中是否存在一个对象。
var estimatedGateDeparture = typeof data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal;
我知道这个数组是空的。我的代码在它吸引上述行时停止运行。我试图测试密钥是否在“对象”中。还是死了。在我看来,如果我使用“typeof”,这将是一个真正安全的方式来看看发生了什么。还是死了。
这仍然无效。当没有任何东西时,它会捕获,但当某些东西存在时,它就不会“看到”它。
if (data.flightStatuses[i].operationalTimes.length > 0)
{var estimatedGateDeparture = data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal;
}
else
{alert ("no statuses");
}
数据子集:所以,我正试图拔出.estimatedGateDeparture.dateLocal
{ “FLIGHTID”:321439750 “carrierFsCode”: “WN”, “FLIGHTNUMBER”: “963”, “departureAirportFsCode”: “BWI”, “arrivalAirportFsCode”: “CHS”, “departureDate”:{ “dateLocal” : “2014-01-12T16:05:00.000”, “dateUtc”: “2014-01-12T21:05:00.000Z”}, “arrivalDate”:{ “dateLocal”:“2014-01-12T17:40:00.000 “ ”dateUtc“: ”2014-01-12T22:40:00.000Z“}, ”状态“: ”S“, ”日程表“:{ ”flightType“: ”J“, ”serviceClasses“: ”RY“,”限制 “:” F “ ”上线“:[{ ”fsCode“: ”MDW“, ”FLIGHTID“:321477764},{ ”fsCode“: ”CLE“, ”FLIGHTID“:321444143},{ ”fsCode“:” BNA “ ”FLIGHTID“:321436891}]}, ”operationalTimes“:{ ”publishedDeparture“:{ ”dateLocal“: ”2014-01-12T16:05:00.000“, ”dateUtc“:” 2014-01-12T21:05 :00.000Z “},” publishedArrival “:{” dateLocal “:” 2014-01-12T17:40:00.000" , “dateUtc”: “2014-01-12T22:40:00.000Z”}, “scheduledGateDeparture”:{ “dateLocal”: “2014-01-12T16:05:00.000”, “dateUtc”: “2014-01-12T21:05:00.000Z”}, “estimatedGateDeparture”:{ “dateLocal”:“2014-01-12T16: 05:00.000" , “dateUtc”: “2014-01-12T21:05:00.000Z”}, “flightPlanPlannedDeparture”:{ “dateLocal”:“2014-01-12T16:15:0 0.000" , “dateUtc”: “2014-01-12T21:15:00.000Z”}, “estimatedRunwayDeparture”:{ “dateLocal”: “2014-01-12T16:15:00.000”, “dateUtc”:“2014-01 -12T21:15:00.000Z “},” scheduledGateArrival “:{” dateLocal “:” 2014-01-12T17:40:00.000" , “dateUtc”: “2014-01-12T22:40:00.000Z”},” estimatedGateArrival “:{” dateLocal “:” 2014-01-12T17:40:00.000" , “dateUtc”: “2014-01-12T22:40:00.000Z”}, “flightPlanPlannedArrival”:{ “dateLocal”:“2014- 01-12T17:35:00.000" , “dateUtc”: “2014-01-12T22:35:00.000Z”}, “estimatedRunwayArrival”:{ “dateLocal”: “2014-01-12T17:35:00.000”,“dateUtc “:” 2014-01-12T22:35:00.000Z “}},” 代码共享 “:[{” fsCode “:” FL “ ”FLIGHTNUMBER“: ”2963“, ”关系“: ”L“}],” flightDurations “:{” scheduledBlockMinutes “:95,” scheduledAirMinutes “:80,” scheduledTaxiOutMinutes “:10”,scheduledTaxiInMinutes “:5},” airportResources “:{” departureGate “:” A3" , “arrivalGate”: “B5”} “flightEquipment”:
答案 0 :(得分:1)
你路上的某个地方是undefined
,即
var x;
var y=0;
console.log(x.a); // error: cannot read property 'a' of undefined
console.log(y.a); // undefined, no error
console.log(z.a); // error: 'z' is not defined
因此,您应该测试路径上的所有对象
var x, estimatedGateDeparture=null;
if(
(x=data) &&
(x=x.flightStatuses) &&
(x=x[i]) &&
(x=x.operationalTimes) &&
(x=x.estimatedGateDeparture)
) estimatedGateDeparture = x.dateLocal;
或者,如果性能不是问题,如果你很懒,只需使用例外:
var estimatedGateDeparture;
try { estimatedGateDeparture = data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal; }
catch(e) { estimatedGateDeparture = null; }
答案 1 :(得分:0)
好的,我想你想要做的就是这个
if (data.flightStatuses.length > i){
var estimatedGateDeparture = data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal;
}
答案 2 :(得分:0)
您可以尝试检查对象的“构造函数”字段返回的值:
var x = new User();
typeof x ==> returns "object"
x.constructor ==> returns "User"
我知道你有一个数组,但你也可以使用这种方法而不是typeof运算符。