Daten aus Json Import zerlegen

Hallo,

ich weis das Thema ist schon mehrfach behandelt, aber ich habe jetzt so viele Beiträge gelesen und komme nicht weiter.

Ich bekomme nach einen Datenimport

$json = json_decode($response, true);

folgende Ausgabe

Array
(
    [devices] => Array
        (
            [2] => Array
                (
                    [instances] => Array
                        (
                            [0] => Array
                                (
                                    [commandClasses] => Array
                                        (
                                            [98] => Array
                                                (
                                                    [name] => DoorLock
                                                    [data] => Array
                                                        (
                                                            [value] => 
                                                            [type] => empty
                                                            [mode] => Array
                                                                (
                                                                    [value] => 254
                                                                    [type] => int
                                                                    [invalidateTime] => 1591446257
                                                                    [updateTime] => 1591458525

Jetzt möchte ich die die Ausgabe von devices[2]->instances[0]->commandClasses[98]->data->mode->value:254; in eine Variable schreiben.

$mode = $json->devices[2]->instances[0]->commandClasses[98]->data->mode->value; 

Hier bekomme ich folgende Meldung und nichts geschieht

Notice:  Trying to get property 'devices' of non-object in /var/lib/symcon/scripts/46278.ips.php on line 106

Notice:  Trying to get property 'instances' of non-object in /var/lib/symcon/scripts/46278.ips.php on line 106

Notice:  Trying to get property 'commandClasses' of non-object in /var/lib/symcon/scripts/46278.ips.php on line 106

Notice:  Trying to get property 'data' of non-object in /var/lib/symcon/scripts/46278.ips.php on line 106

Notice:  Trying to get property 'mode' of non-object in /var/lib/symcon/scripts/46278.ips.php on line 106

Notice:  Trying to get property 'value' of non-object in /var/lib/symcon/scripts/46278.ips.php on line 106

Ich habe schon vieles probiert - ohne Erfolg. Wer kann mir weiterhelfen?

Gruß Thomas

Du hast dir beim json_decode ein Array zurückgeben lassen.
Beim Zugriff auf die Elemente nutzt du aber die für Objekte gültige Notation.

Richtig wenn Du den Parameter true setzt bekommst Du einen Array, Du must Dich also entscheiden ob Du ein Objekt oder ein Array ansprechen willst.

Das kann nicht funktionieren wenn Du das wie ein Objekt mit → ansprechen willst, wenn Du das aber als Array ausgeben lässt weil Du ja true gesetzt hast (s.o.). Wenn Das ein Array ist musst Du das auch als Array auslesen also

$mode = $json['devices'][2]['instances'][0]['commandClasses'][98]['data']['mode']['value']; 

Danke für die Hilfe aber es wirft immer wieder Fehler aus.
Die Zeile

$mode = $json->['devices'][2]['instances'][0]['commandClasses'][98]['data']['mode']['value']; 

bringt die Meldung

Parse error:  syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /var/lib/symcon/scripts/46278.ips.php on line 106
$mode = $json->['devices'][2]['instances'][0]['commandClasses'][98]['data']['mode']['value'];  

Könnte sich da noch ein „->“ nach $json eingeschlichen haben?:slight_smile:

$mode = $json['devices'][2]['instances'][0]['commandClasses'][98]['data']['mode']['value'];  

Hallo Rainer,

Danke - genau das war es.:slight_smile:

Gruß Thomas