Skip to content
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Standard: (Kein Skin)
  • Kein Skin
Einklappen
ioBroker Logo
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. [gelöst] Bitte um Hilfe bei JSON.parse bzw. Array handling

NEWS

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    8.1k

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    13
    1
    1.8k

  • Neues Video "KI im Smart Home" - ioBroker plus n8n
    BluefoxB
    Bluefox
    15
    1
    2.1k

[gelöst] Bitte um Hilfe bei JSON.parse bzw. Array handling

Geplant Angeheftet Gesperrt Verschoben JavaScript
javascript
5 Beiträge 2 Kommentatoren 558 Aufrufe 1 Watching
  • Älteste zuerst
  • Neuste zuerst
  • Meiste Stimmen
Antworten
  • In einem neuen Thema antworten
Anmelden zum Antworten
Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
  • S Offline
    S Offline
    stimezo
    Forum Testing
    schrieb am zuletzt editiert von stimezo
    #1

    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.trackNumber
    

    Aber wie komme ich an das Gate?

    flugplan[0].flight.arrival.gates.gate  
    

    funktioniert nicht (undefined).

    Hat da jemand eine Idee?

    Danke
    Christian

    1 Antwort Letzte Antwort
    0
    • AsgothianA Offline
      AsgothianA Offline
      Asgothian
      Developer
      schrieb am zuletzt editiert von
      #2

      Versuchs mal mit

      flugplan[0].flight.arrival.gates[0].gate
      

      ioBroker auf RPi4 - Hardware soweit wie möglich via Zigbee.
      "Shit don't work" ist keine Fehlermeldung, sondern ein Fluch.

      S 1 Antwort Letzte Antwort
      1
      • AsgothianA Asgothian

        Versuchs mal mit

        flugplan[0].flight.arrival.gates[0].gate
        
        S Offline
        S Offline
        stimezo
        Forum Testing
        schrieb am zuletzt editiert von
        #3

        @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

        1 Antwort Letzte Antwort
        0
        • S Offline
          S Offline
          stimezo
          Forum Testing
          schrieb am zuletzt editiert von stimezo
          #4

          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:114
          

          Da noch eine Idee?

          Danke
          Christian

          1 Antwort Letzte Antwort
          0
          • S Offline
            S Offline
            stimezo
            Forum Testing
            schrieb am zuletzt editiert von
            #5

            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

            1 Antwort Letzte Antwort
            0
            Antworten
            • In einem neuen Thema antworten
            Anmelden zum Antworten
            • Älteste zuerst
            • Neuste zuerst
            • Meiste Stimmen


            Support us

            ioBroker
            Community Adapters
            Donate

            753

            Online

            32.4k

            Benutzer

            81.4k

            Themen

            1.3m

            Beiträge
            Community
            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
            ioBroker Community 2014-2025
            logo
            • Anmelden

            • Du hast noch kein Konto? Registrieren

            • Anmelden oder registrieren, um zu suchen
            • Erster Beitrag
              Letzter Beitrag
            0
            • Aktuell
            • Tags
            • Ungelesen 0
            • Kategorien
            • Unreplied
            • Beliebt
            • GitHub
            • Docu
            • Hilfe