Skip to content
  • Home
  • 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
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. [gelöst] Bitte um Hilfe bei JSON.parse bzw. Array handling

NEWS

  • Neuer ioBroker-Blog online: Monatsrückblick März/April 2026
    BluefoxB
    Bluefox
    3
    1
    79

  • Verwendung von KI bitte immer deutlich kennzeichnen
    HomoranH
    Homoran
    8
    1
    130

  • Monatsrückblick Januar/Februar 2026 ist online!
    BluefoxB
    Bluefox
    18
    1
    861

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

Scheduled Pinned Locked Moved JavaScript
javascript
5 Posts 2 Posters 619 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

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            Support us

            ioBroker
            Community Adapters
            Donate

            390

            Online

            32.8k

            Users

            82.7k

            Topics

            1.3m

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

            • Don't have an account? Register

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