Skip to content
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • 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

  • Default (No Skin)
  • No Skin
Collapse
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

  • Wartung am 15.11. – Forum ab 22:00 Uhr nicht erreichbar
    BluefoxB
    Bluefox
    11
    2
    209

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

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

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

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

Scheduled Pinned Locked Moved JavaScript
javascript
5 Posts 2 Posters 558 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stimezo
    Forum Testing
    wrote on last edited by 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 Reply Last reply
    0
    • AsgothianA Offline
      AsgothianA Offline
      Asgothian
      Developer
      wrote on last edited by
      #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 Reply Last reply
      1
      • AsgothianA Asgothian

        Versuchs mal mit

        flugplan[0].flight.arrival.gates[0].gate
        
        S Offline
        S Offline
        stimezo
        Forum Testing
        wrote on last edited by
        #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 Reply Last reply
        0
        • S Offline
          S Offline
          stimezo
          Forum Testing
          wrote on last edited by 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 Reply Last reply
          0
          • S Offline
            S Offline
            stimezo
            Forum Testing
            wrote on last edited by
            #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 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            Support us

            ioBroker
            Community Adapters
            Donate

            103

            Online

            32.4k

            Users

            81.3k

            Topics

            1.3m

            Posts
            Community
            Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
            ioBroker Community 2014-2025
            logo
            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Recent
            • Tags
            • Unread 0
            • Categories
            • Unreplied
            • Popular
            • GitHub
            • Docu
            • Hilfe