LiTHiUM2525 LiTHiUM2525 -4 years ago 175
JSON Question

PHP JSon How to read boolean value received in JSon format and write in string on PHP

I receive this JSON string from another site and I cannot modify what received from. The string is receive in $_POST and is :

[
{
"clientId":"17295c59-4373-655a-1141-994aec1779dc",
"channel":"\/meta\/connect",
"connectionType":"long-polling",
"ext":{
"fm.ack":false,
"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"
},
"id":"5"
}
]


I decode the JSON string with the following code :

$receive = json_decode(file_get_contents('php://input'));


And when I use
print_r($receive)
I get the following:

Array (
[0] => stdClass Object
(
[clientId] => 17295c59-4373-655a-1141-994aec1779dc
[channel] => /meta/connect
[connectionType] => long-polling
[ext] => stdClass Object
(
[fm.ack] =>
[fm.sessionId] => 22b0bdcf-4a35-62fc-3764-db4caeece44b
)

[id] => 5
)
)


I can access and read all Array / Object with no problem :

$receive[$i]->clientId;
$receive[$i]->channel;
$connectionType = $receive[$i]->connectionType;
$receive[$i]->id;
$receive[$i]->ext->{'fm.sessionId'};


But {fm.ack} is empty

In the decoded JSON string, the false value is not between
""
.

Is it possible to access and read the false value and convert it into string value instead?

Thank you for your helping !

Answer Source

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"\/meta\/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}

working example for false http://codepad.viper-7.com/7FBQfS

working example for true http://codepad.viper-7.com/LX0pc6

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download