9

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 !

1
  • what is the behaviour if there is true in the JSON ? Commented Feb 26, 2013 at 4:20

3 Answers 3

11

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";   
}
Sign up to request clarification or add additional context in comments.

7 Comments

PHP also has a false. Why not compare against that? That's what the decoded JSON array contains.
@mario Yeah we can also comapare with false. Thanks for pointing out.
so if i try : if($receive[0]->ext->{'fm.ack'} == "") { echo "false"; } else { echo "true"; } it's work !!! Thank you !
@YogeshSuthar yes i know it so is working only if i hardcode my JSon string, not working when i receive JSon string on $_POST the only code can read the string is $receive = json_decode(file_get_contents('php://input')); and not work if i add ,true... so your solution to test if == "" for false else true working well. Thank you
This is so ghetto. Running into this problem right now. In what world should a valid JSON value of "false" be returned as "blank"?
|
5

I know there is already an answer to this but it may be worth noting that var_dump outputs Boolean values better it just has worse formatting IMO.

<pre>
    <?php
        print_r(array(true, false));
        var_dump(array(true, false));
    ?>
</pre>

Results in

Array
(
    [0] => 1
    [1] => 
)
array(2) {
  [0]=>
  bool(true)
  [1]=>
  bool(false)
}

1 Comment

This should be the accepted answer. Booleans were there all along, it's just that print_r() doesn't show them correctly.
1

It's a very simple. If you use js to generate and transfer json, pass your variable not in its pure form but: youBoolVar + 0, then false will be 0, and true 1

1 Comment

this seems like the simplest answer. worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.