简体   繁体   English

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

[英]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.我从另一个站点收到此 JSON 字符串,但无法修改收到的内容。 The string is receive in $_POST and is :该字符串在 $_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 !谢谢你的帮助!

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 .你可以像这样使用它,在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";   
}

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.我知道已经有一个答案,但可能值得注意的是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)
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM