PHP JSon如何读取以JSon格式接收的布尔值并在PHP上以字符串形式写入 [英] PHP JSon How to read boolean value received in JSon format and write in string on PHP

查看:213
本文介绍了PHP JSon如何读取以JSon格式接收的布尔值并在PHP上以字符串形式写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从另一个站点接收到此JSON字符串,因此无法修改从那里接收到的JSON字符串.该字符串在$ _POST中接收,并且为:

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"
    }
]

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

I decode the JSON string with the following code :

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

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

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'};

但是{fm.ack}是空的

But {fm.ack} is empty

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

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?

谢谢您的帮助!

推荐答案

您可以像这样使用它,当您评估false值时,它以JSON格式提供给您blank,而当您评估它会给您1.

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";   
}

错误 http://codepad.viper-7.com/7FBQfS 的工作示例

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

真正的 http://codepad.viper-7.com/LX0pc6 的工作示例

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

这篇关于PHP JSon如何读取以JSon格式接收的布尔值并在PHP上以字符串形式写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆