Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JSON Array in nodejs Objekt umwandeln

    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

    JSON Array in nodejs Objekt umwandeln

    This topic has been deleted. Only users with topic management privileges can see it.
    • X
      XerXes777 last edited by

      Moin Moin,

      wie kann ich aus diesem JSON,

      nodejs Datenpunkte erstellen?

      {"statusCode":200,"body":"[{\"error\":{\"type\":3,\"address\":\"/lights/30\",\"description\":\"resource, /lights/30, not available\"}}]","headers":{"server":"nginx","date":"Wed, 16 Jan 2019 22:33:24 GMT","content-type":"application/json","connection":"close","cache-control":"no-store, no-cache, must-revalidate, post-check=0, pre-check=0","pragma":"no-cache","expires":"Mon, 1 Aug 2011 09:00:00 GMT","access-control-max-age":"3600","access-control-allow-origin":"*","access-control-allow-credentials":"true","access-control-allow-methods":"POST, GET, OPTIONS, PUT, DELETE, HEAD","access-control-allow-headers":"Content-Type"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"IP","port":80,"hostname":"IP","hash":null,"search":null,"query":null,"pathname":"/api/KEY/lights/30","path":"/api/KEY/lights/30","href":"http://IP/api/KEY/lights/30"},"method":"GET","headers":{}}}
      

      Ich brauche:

      type:

      address:

      description:

      Dies funktioniert:

      console.warn(response.statusCode)
      

      Wenn ich:

      console.warn(response.body)
      

      eingebe bekomme ich den kompletten array als string ausgegeben.

      Wenn ich:

      console.warn(response.body[0]['error'].type)
      

      oder

      console.warn(response.body[0].error.type)
      

      probiere bekomme ich einen TypeError.

      Kann mir da jemand helfen?

      Mit freundlichen Grüßen

      Kevin

      1 Reply Last reply Reply Quote 0
      • apollon77
        apollon77 last edited by

        In Body steht es als String.

        const body = JSON.parse(response.body);

        Dann kannst du drauf zugreifen.

        Gesendet vom Handy …

        1 Reply Last reply Reply Quote 0
        • X
          XerXes777 last edited by

          Aber meine response ist ein JSON.

          Ich habe nur mit:

          console.log(JSON.stringify(response));
          

          lesbar im LOG gemacht und hier rein kopiert.

          1 Reply Last reply Reply Quote 0
          • Lenny.CB
            Lenny.CB Most Active last edited by

            @XerXes777:

            Aber meine response ist ein JSON.

            Ich habe nur mit:

            console.log(JSON.stringify(response));
            

            lesbar im LOG gemacht und hier rein kopiert. `

            Ich bin auch grad am Lernen, hatte vor Kurzem ein ähnliches Problem:

            ich vermute, deine Frunktion sieht so aus: function (error, response, body) {

            wenn dein body so ausschaut:

            {"error":{"type":3,"address":"/lights/30","description":"resource, /lights/30, not available"}}
            

            dann versuche mal:

            var obj = JSON.parse(body);
            
            log('Type: ' + obj[Object.keys(obj)[0]].type);
            log('Address: ' + obj[Object.keys(obj)[0]].address);
            log('Description: ' + obj[Object.keys(obj)[0]].description);
            
            1 Reply Last reply Reply Quote 1
            • First post
              Last post

            Support us

            ioBroker
            Community Adapters
            Donate

            822
            Online

            31.7k
            Users

            79.9k
            Topics

            1.3m
            Posts

            3
            4
            689
            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