Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JSON.stringify zerlegen

    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.stringify zerlegen

    This topic has been deleted. Only users with topic management privileges can see it.
    • Lenny.CB
      Lenny.CB Most Active last edited by

      Hallo zusammen,

      komme grad nicht weiter und bräuchte mal einen Tipp.

      ich mache einen "request.get" und bekomme mit JSON.stringify(body) folgendes Ergebnis.

      body: {"000147619644":{"ident":{"type":{"key_localized":"Devicetype","value_raw":1,"value_localized":"Washing Machine"},"deviceName":"","deviceIdentLabel":{"fabNumber":"000147619644","fabIndex":"11","techType":"WMV963","matNumber":"10499100","swids":["3814","25105","25117","20163","3627","20242","25042","25043","25026","2971","20166"]},"xkmIdentLabel":{"techType":"XKM3100W","releaseVersion":"02.45"}},"state":{"status":{"value_raw":1,"value_localized":"Off","key_localized":"State"},"programType":{"value_raw":0,"value_localized":"","key_localized":"Programme"},"programPhase":{"value_raw":256,"value_localized":"","key_localized":"Phase"},"remainingTime":[0,0],"startTime":[0,0],"targetTemperature":[{"value_raw":-32768,"value_localized":null,"unit":"Celsius"}],"temperature":[{"value_raw":-32768,"value_localized":null,"unit":"Celsius"},{"value_raw":-32768,"value_localized":null,"unit":"Celsius"},{"value_raw":-32768,"value_localized":null,"unit":"Celsius"}],"signalInfo":false,"signalFailure":false,"signalDoor":false,"remoteEnable":{"fullRemoteControl":true,"smartGrid":true},"light":0,"elapsedTime":[0,0],"spinningSpeed":0,"dryingStep":{"value_raw":null,"value_localized":"","key_localized":"Drying level"},"ventilationStep":{"value_raw":null,"value_localized":"","key_localized":"Power Level"}}}}
      

      Hier will ich nun nach einzelnen Schlüsselwörtern suchen und die Keys dazu speichern.

      Komme hier grad nicht mehr klar.

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

        Normalerweise wird in body ein JSON-String geliefert, den man per JSON.parse(body) in ein Objekt wandeln muss.

        Den http://jsonviewer.stack.hu/ kennst Du ?

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

          Wenn aber das Stringify so aussieht wie oben dann ist das schon ein in ein Objekt geparstes JSON, kann also direkt verwendet werden …

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

            @paul53:

            Normalerweise wird in body ein JSON-String geliefert, den man per JSON.parse(body) in ein Objekt wandeln muss.

            Den http://jsonviewer.stack.hu/ kennst Du ? ` Das ist nen bissel komisch…

            Das ist son ein "3er-ping-pong"

            1. Code per request.post abfragen. Was ich brauche hole ich mit: JSON.stringify(response.header)

            2. Token per request.post holen.

            Was ich brauche hole ich mit: JSON.parse(body)

            3. Divices holen

            Was ich brauche hole ich mit: JSON.stringify(body)

            Den link schaue ich mir mal an.

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

              @apollon77:

              Wenn aber das Stringify so aussieht wie oben dann ist das schon ein in ein Objekt geparstes JSON, kann also direkt verwendet werden … ` Und genau da fehlts mir… schon viel gelesen und probiert, nix geklappt.

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

                @Lenny.CB:

                2. Token per request.post holen.

                Was ich brauche hole ich mit: JSON.parse(body)

                3. Divices holen

                Was ich brauche hole ich mit: JSON.stringify(body) `
                Was nun ? Einfach testen mit

                log('Typ body: ' + typeof body);
                
                1 Reply Last reply Reply Quote 0
                • Lenny.CB
                  Lenny.CB Most Active last edited by

                  @paul53:

                  @Lenny.CB:

                  2. Token per request.post holen.

                  Was ich brauche hole ich mit: JSON.parse(body)

                  3. Divices holen

                  Was ich brauche hole ich mit: JSON.stringify(body) `
                  Was nun ? Einfach testen mit

                  log('Typ body: ' + typeof body);
                  ```` `  
                  

                  genau wie ich geschrieben habe.

                  Beim Token (request.post) kommt ein "Typ body Token: string" zurück,

                  Beim Device (request.get) kommt ein "Typ body Device: object" zurück. Warum auch immer.

                  Mit der Token-Antwort komme ich zurecht. Da hole ich das raus mit:

                  let obj = JSON.parse(body);
                  access_token = obj.access_token;
                  

                  Nur wenn halt ein Objekt zurück kommt, komme ich nicht klar. Schaut auch irgendwie "verschachtelt" aus.

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

                    @Lenny.CB:

                    Nur wenn halt ein Objekt zurück kommt, komme ich nicht klar. Schaut auch irgendwie "verschachtelt" aus. `
                    Ist es das Objekt aus dem ersten Beitrag ? Im JSON-Viewer sieht es so aus (ohne "body: "):
                    493_json_lenny.jpg
                    Der Zugriff auf z.B. state.status.value_raw würde so erfolgen:

                    var status_value_raw = obj["000147619644"].state.status.value_raw;
                    
                    1 Reply Last reply Reply Quote 0
                    • Lenny.CB
                      Lenny.CB Most Active last edited by

                      @paul53:

                      Ist es das Objekt aus dem ersten Beitrag ? ` ja korrekt
                      @paul53:

                      Der Zugriff auf z.B. state.status.value_raw würde so erfolgen:

                      var status_value_raw = obj["000147619644"].state.status.value_raw;
                      ```` `  
                      

                      dabei bekomme ich folgenden Fehler:

                      javascript.2 TypeError: Cannot read property 'status' of undefined at Request._callback (script.js.Testscripte.Miele_Abfrage:94:57) at Request.self.callback (/opt/iobroker/node_modules/iobroker.javascript/node_modules/request/request.js:185:22) at emitTwo (events.js:106:13) at Request.emit (events.js:191:7) at Request. (/opt/iobroker/node_modules/iobroker.javascript/node_modules/request/request.js:1161:10) at emitOne (events.js:96:13) at Request.emit (events.js:188:7) at IncomingMessage. (/opt/iobroker/node_modules/iobroker.javascript/node_modules/request/request.js:1083:12) at IncomingMessage.g (events.js:292:16) at emitNone (events.js:91:20)
                      Caught by controller[0]: at Request._callback (script.js.Testscripte.Miele_Abfrage:94:57)
                      
                      1 Reply Last reply Reply Quote 0
                      • paul53
                        paul53 last edited by

                        @Lenny.CB:

                        dabei bekomme ich folgenden Fehler: `
                        ??
                        493_objekt_lenny.jpg

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

                          wenn ich das so aufbaue:var obj = {"000147619644":{"ident":{"type":{"key_localized":"Devicetype","value_raw":1,"value_localized":"Washing Machine"},"deviceName":"","deviceIdentLabel":{"fabNumber":"000147619644","fabIndex":"11","techType":"WMV963","matNumber":"10499100","swids":["3814","25105","25117","20163","3627","20242","25042","25043","25026","2971","20166"]},"xkmIdentLabel":{"techType":"XKM3100W","releaseVersion":"02.45"}},"state":{"status":{"value_raw":1,"value_localized":"Off","key_localized":"State"},"programType":{"value_raw":0,"value_localized":"","key_localized":"Programme"},"programPhase":{"value_raw":256,"value_localized":"","key_localized":"Phase"},"remainingTime":[0,0],"startTime":[0,0],"targetTemperature":[{"value_raw":-32768,"value_localized":null,"unit":"Celsius"}],"temperature":[{"value_raw":-32768,"value_localized":null,"unit":"Celsius"},{"value_raw":-32768,"value_localized":null,"unit":"Celsius"},{"value_raw":-32768,"value_localized":null,"unit":"Celsius"}],"signalInfo":false,"signalFailure":false,"signalDoor":false,"remoteEnable":{"fullRemoteControl":true,"smartGrid":true},"light":0,"elapsedTime":[0,0],"spinningSpeed":0,"dryingStep":{"value_raw":null,"value_localized":"","key_localized":"Drying level"},"ventilationStep":{"value_raw":null,"value_localized":"","key_localized":"Power Level"}}}}; log(obj["000147619644"].state.status.value_raw);klappte es bei mir auch.````
                          16:07:36.884 [info] javascript.2 script.js.Testscripte.Miele_device_test: 1

                          
                          Habe heut Morgen noch etwas versucht und wieder mal gesehen, dass ich mir selbst ins Knie geschossen habe. Is halt so wenn man nicht wirklich weiß was im kopierten Code passiert (Quasi wie immer :shock: ) Muss hier noch weiter lernen. :ugeek:
                          
                          hier mal der Code:````
                          function Device_Abfrage () {
                                  request.get    ({  
                                                  url: 'https://api.mcs3.miele.com/v1/devices/',
                                                  // json: true,
                                                  headers: {'Authorization': 'Bearer xxxxxxxx86816993400b8b44xxxxxxxx'}
                                                  },
                                                  function (error, response, body) {
                                                  log('Device_statusCode: ' + response.statusCode);
                                                  log('body: ' + body);
                                                  log('Device_error: ' + error);
                                                  log('Typ body Device: ' + typeof body);
                                                  var obj = JSON.parse(body);
                                                  log(obj["000147619644"].state.status.value_raw);
                          });    
                          }
                          
                          Device_Abfrage ();
                          

                          ich hatte "json: true" mitgegeben. Antwort kam das als Objekt zurück. Konnte es aber nicht mit JSON.stringify verarbeiten.

                          "json: true" auskommentiert, schon kam ein String zurück den ich mit JSON.parse bearbeiten konnte.

                          @paul53, danke für den Denkanstoß!

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post

                          Support us

                          ioBroker
                          Community Adapters
                          Donate

                          916
                          Online

                          31.7k
                          Users

                          79.9k
                          Topics

                          1.3m
                          Posts

                          3
                          11
                          2467
                          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