NEWS
[gelöst] Bitte um Hilfe bei JSON.parse bzw. Array handling
-
Hallo Gemeinde,
ich stehe gerade etwas auf dem Schlauch

Ich habe ein JSON, welches ich Parse, hier ein Ausschnitt:
[ { "flight": { "operatingAirline": { "iataCode": "NH", "icaoCode": "ANA", "name": "ANA All Nippon Airways", "airlineDisplayCode": "NH" }, "serviceTypeCode": "PP", "aircraftType": { "icaoCode": "B77W", "modelName": "B77W", "registration": "JA791A" }, "flightNumber": { "airlineCode": "NH", "trackNumber": "203", "suffix": "" }, "codeShares": [ { "codeShare": { "airlineCode": "LH", "trackNumber": "4921", "suffix": "" } } ], "departureAirport": "HND", "arrivalAirport": "FRA", "originDate": "2019-04-12", "arrival": { "scheduled": "2019-04-12T03:20:00Z", "estimated": "2019-04-12T03:17:00Z", "actual": "2019-04-12T03:16:12Z", "terminal": "1", "gates": [ { "gate": "B45A" } ], "baggageClaim": [ { "carousel": "18" } ] } } } ]Wenn ich jetzt parse:
flugplan = JSON.parse(flugplan);Dann finde ich bspw. die Flugnummer unter:
flugplan[0].flight.flightNumber.trackNumberAber wie komme ich an das Gate?
flugplan[0].flight.arrival.gates.gatefunktioniert nicht (undefined).
Hat da jemand eine Idee?
Danke
Christian -
Versuchs mal mit
flugplan[0].flight.arrival.gates[0].gate -
@Asgothian sagte in Bitte um Hilfe bei JSON.parse bzw. Array handling:
flugplan[0].flight.arrival.gates[0].gate
Perfekt,
vielen Dank für die schnelle Hilfe.Gruss
Christian -
Eine Frage hätte ich noch

Ich lasse das erzeugte Array durch eine Schleife laufen um eine HTML-Tabelle zu generieren.
Jetzt ist aber nicht in jeder Abfrage das Gate bereits erhalten.
Um nicht in einen Fehler zu laufen, muss ich also prüfen, ob das Element existiert,
Das gelingt mir hier nicht.Wenn ich (Codeauszug):
if (typeof flugplan[i].flight.arrival.gates[0].gate !== 'undefined') { arrivalshtml=arrivalshtml + '<th class="tg-2uhc">'; arrivalshtml=arrivalshtml + flugplan[i].flight.arrival.gates[0].gate; arrivalshtml=arrivalshtml + '</th>'; } else { arivalshtml=arrivalshtml + '<th class="tg-2uhc">'; arrivalshtml=arrivalshtml + " "; arrivalshtml=arrivalshtml + '</th>'; //}Diese Abfrage, die bei den anderen Einträgen funktioniert, läuft
sobald ein Datensatz ohne Gate kommt in einen Fehler:javascript.0 2019-04-12 21:27:57.361 error at ContextifyScript.Script.runInContext (vm.js:59:29) javascript.0 2019-04-12 21:27:57.361 error at script.js.common.Flughafen.FRAarrivals:114:44 javascript.0 2019-04-12 21:27:57.361 error TypeError: Cannot read property '0' of undefined javascript.0 2019-04-12 21:27:57.361 error ^ javascript.0 2019-04-12 21:27:57.361 error if (typeof flugplan[i].flight.arrival.gates[0] !== 'undefined') { javascript.0 2019-04-12 21:27:57.361 error script.js.common.Flughafen.FRAarrivals: script.js.common.Flughafen.FRAarrivals:114Da noch eine Idee?
Danke
Christian -
So, gelöst

falls jemand mal über so was stolpert:
try { if(typeof flugplan[i].flight.arrival.gates[0].gate == 'undefined') { // does not exist arivalshtml=arrivalshtml + '<th class="tg-2uhc">'; arrivalshtml=arrivalshtml + " "; arrivalshtml=arrivalshtml + '</th>'; } else { // does exist arrivalshtml=arrivalshtml + '<th class="tg-2uhc">'; arrivalshtml=arrivalshtml + flugplan[i].flight.arrival.gates[0].gate; arrivalshtml=arrivalshtml + '</th>'; } } catch (error){ /* ignore */ }macht den Trick.
Viele Grüße
Christian