PHP JSon 如何读取以 JSon 格式接收的布尔值并在 PHP 上写入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15081227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:31:51  来源:igfitidea点击:

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

phpjson

提问by LiTHiUM2525

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

我从另一个站点收到此 JSON 字符串,但无法修改收到的内容。该字符串在 $_POST 中接收并且是:

[
    {
        "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 :

我使用以下代码解码 JSON 字符串:

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

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

当我使用时,print_r($receive)我得到以下信息:

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

但是 {fm.ack} 是空的

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

在解码后的 JSON 字符串中,false 值不在 之间""

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

是否可以访问和读取假值并将其转换为字符串值?

Thank you for your helping !

谢谢你的帮助!

回答by Yogesh Suthar

you can use it like this, in JSONformat when you evaluate falsevalue it will give you blank, and when you evaluate trueit will give you 1.

你可以像这样使用它,在JSON你评估false价值时它会给你的格式blank,当你评估true它时会给你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";   
}

回答by Christian

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

我知道已经有一个答案,但可能值得注意的是,var_dump输出布尔值更好,只是 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)
}

回答by Павел Ковалёв

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

这是一个非常简单的。如果您使用 js 生成和传输 json,请不要以纯形式传递您的变量,而是:youBoolVar + 0,则 false 将为 0,true 为 1