nothing happening while decoding json data

时间:2018-02-01 18:40:14

标签: php json jsondecoder jsonencoder

Im trying to decode the following json data,
NOTE, the data has a weird attribute a '\' in front of each key.

{"data":"{\"product_data\":{\"LXR12-3\":0,\"GXPI01\":1,\"LXR12-1\":1,\"LXR12-2\":1},\"access_token\":\"1M6opdQlMXygxowkn8uOgsEYNUGcXKF4UcuMX0fzwpxmZT4Hn7\",\"intent\":\"okay\",\"update\":false}"}

PHP

$jsonRaw = file_get_contents('php://input');
file_put_contents('RAW.txt', $jsonRaw);
$data = json_decode($jsonRaw,true);
file_put_contents('DATA.txt', $data);
$access_token = $data['access_token'];  
$intent = $data['intent'];  
$isEdit = $data['update'];  


$text = "access_token : $access_token | intent : $intent | edit : $isEdit";
file_put_contents('GOT_DATA.txt', $text);

The output of the 'GOT_DATA' file is,

access_token :  | intent :  | edit :  

The output for the DATA.txt file is,

{"product_data":{"LXR12-3":0,"GXPI01":1,"LXR12-1":1,"LXR12-2":1},"access_token":"1M6opdQlMXygxowkn8uOgsEYNUGcXKF4UcuMX0fzwpxmZT4Hn7","intent":"okay","update":false}

The values are not begin stored into the variables? What exactly am i doing wrong here?
and does anyone know why the recvied json have a '\' in front?

UPDATE

After changing the source of the JSON i managed to return it like (RAW.txt),

{"data":{"product_data":{"LXR12-2":0,"GXPI01":0,"LXR12-3":1,"LXR12-1":1},"access_token":"1M6opdQlMXygxF4UcuMX0fzwpxmZT4Hn7","intent":"okay","update":false}}

Now the outputs for the files for DATA.txt,

Array

It just says 'Array' Why???

0 个答案:

没有答案