0

I want to decode this data into a php obj, but this is not working. If I run the code it gives nothing.

$Data = "{"3":false,"5":false,"6":true}" 

$obj = json_decode($Data, true);
echo $obj["3"];
echo $obj["5"];

Look at the below code.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://demo.thingsboard.io:443/api/plugins/rpc/twoway/67cadda0-b8c0-11e8-a04e-eb5f6cd0fada");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"method\": \"setGpioStatus\", \"params\": {\"pin\": \"".$PIN_NUMBER."\", \"enabled\": \"".$PIN_STATUS."\"}, \"timeout\": \"500\"}");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "Accept: */*";
$headers[] = "X-Authorization: Bearer ".$New_JWT_TOKEN."";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$GPIO_RESULT = curl_exec($ch); // curl_exec($ch) gives {"3":false,"5":false,"6":true}
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

$obj = json_decode($GPIO_RESULT, true);
echo $obj["3"]; // output: null 

echo $GPIO_RESULT;  // output: {"3":false,"5":false,"6":true}
}

Now I get the output of $GPIO_RESULT but not the output of $obj["3"] or any single digit integer. I found that no integer value is accepted at this format. Can anyone tell me any other way to converting this data.

pushkin
  • 9,575
  • 15
  • 51
  • 95
Ahmad
  • 1
  • 3
  • Welcome. `$Data = "{"3":false,"5":false,"6":true}" ` is an invalid statement. Try `$Data = '{"3":false,"5":false,"6":true}';`. Also see the [documentation about strings](http://php.net/manual/en/language.types.string.php). – Jeto Sep 26 '18 at 18:49
  • Oh, and if you're trying to echo `false` you won't see anything. Try `var_dump` instead. – Jeto Sep 26 '18 at 18:51
  • 1
    Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Madhur Bhaiya Sep 26 '18 at 18:52
  • Data is simply {"3":false,"5":false,"6":true} . I get it from server response using curl command. $data = curl_exec($ch); // curl_exec($ch) output is {"3":false,"5":false,"6":true} – Ahmad Sep 26 '18 at 18:53
  • you do not have the key `"2";` in your json, probably you have surpressed error messages in php.ini, also you can get last json error using `json_last_error` and `json_last_error_msg` functions – rgen3 Sep 26 '18 at 18:56
  • You should probably show your _actual_ code, as this doesn't seem to be representing your problem well. – Patrick Q Sep 26 '18 at 19:02
  • 1
    You are essentially doing `echo false;` which will not produce any output. Try `var_dump($obj["3"]);` – Patrick Q Sep 26 '18 at 19:07
  • Thanks, var_dump() works but it gives result in boolen formate like. echo var_dump($obj["3"]); // output: bool(false) echo $obj["6"]; // output: 1 echo $GPIO_RESULT; // total output: bool(false) 1{"3":false,"5":false,"6":true} var_dump($obj["3"]) output is bool(false) but I Need just "false" output – Ahmad Sep 26 '18 at 19:16
  • 1
    Possible duplicate of [PHP - Get bool to echo false when false](https://stackoverflow.com/questions/4948663/php-get-bool-to-echo-false-when-false) – iainn Sep 26 '18 at 19:19

0 Answers0