Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [gelöst] Bitte um Hilfe bei JSON.parse bzw. Array handling

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

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

    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      stimezo Forum Testing last edited by stimezo

      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 Reply Quote 0
      • Asgothian
        Asgothian Developer last edited by

        Versuchs mal mit

        flugplan[0].flight.arrival.gates[0].gate
        
        S 1 Reply Last reply Reply Quote 1
        • S
          stimezo Forum Testing @Asgothian last edited by

          @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 Reply Quote 0
          • S
            stimezo Forum Testing last edited by stimezo

            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 Reply Quote 0
            • S
              stimezo Forum Testing last edited by

              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 Reply Quote 0
              • First post
                Last post

              Support us

              ioBroker
              Community Adapters
              Donate

              867
              Online

              31.8k
              Users

              80.0k
              Topics

              1.3m
              Posts

              javascript
              2
              5
              466
              Loading More Posts
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes
              Reply
              • Reply as topic
              Log in to reply
              Community
              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
              The ioBroker Community 2014-2023
              logo