小编典典

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

json

我从另一个站点接收到此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"
    }
]

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

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

当我使用时,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
    )
)

我可以毫无问题地访问和读取所有数组/对象:

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

但是{fm.ack}为空

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

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

谢谢您的帮助!


阅读 467

收藏
2020-07-27

共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";   
}
2020-07-27