json_decode()的奇怪行为

时间:2010-04-09 09:34:25

标签: php json

获取以下JSON字符串(由某些ExtJS代码生成 - 但这无关紧要):

[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"type":"rpc","tid":3}]

作为POST请求发送到服务器,并通过$GLOBALS['HTTP_RAW_POST_DATA']检索。

运行

json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
我们的开发机器上的

(带有Suhosin Patch的5.2.10-2ubuntu6.4 0.9.7)提供了正确的print_r()输出:

Array
(
    [0] => stdClass Object
        (
            [action] => Setting
            [method] => toggle
            [data] => Array
                (
                    [0] => welcome-home
                )

            [type] => rpc
            [tid] => 2
        )

    [1] => stdClass Object
        (
            [action] => ContentExtFeFillout
            [method] => todo
            [data] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 8
                    [3] =>
                )

            [type] => rpc
            [tid] => 3
        )
)

在客户端的生产计算机上运行相同的代码(5.2.5使用Suhosin Patch 0.9.6.2和Zend Optimizer;顺便说一下SUSE Linux)提供以下print_r()输出:

Array
(
    [0] => stdClass Object
        (
            [action] => Setting
            [method] => toggle
            [data] => Array
                (
                    [0] => welcome-home
                )

            [type] => rpc
        )

    [1] => 2
    [2] => stdClass Object
        (
            [action] => ContentExtFeFillout
            [method] => todo
            [data] => Array
                (
                    [0] => 1
                    [1] => 0
                    [2] => 8
                    [3] => 
                )

            [type] => rpc
        )

    [3] => 3
)

请注意缺少的 tid 属性,该属性显然已作为自己的值移入主数组中 - 这自然会破坏以下所有代码。

我们还下载了一个Windows PHP版本5.2.5以检查json_decode()中是否存在错误,但我们在此处获得了正确的输出。

json_decode()是否存在可能导致这种奇怪行为的已知问题?

我们目前完全无能为力......

感谢大家!

祝你好运

的Stefan

2 个答案:

答案 0 :(得分:1)

好的伙计们 - 问题解决了。由于缺乏更多选项,我们说服客户更新已安装的PHP版本,并猜测:它有效。

他们的PHP安装(PHP,Zend Optimizer和/或Suhosin)中似乎存在一个微妙的错误,该错误已通过更新修复。仍然是一件非常奇怪的事情。

感谢大家!

祝你好运

的Stefan

答案 1 :(得分:0)

您是否尝试过交换tid和键入密钥?另外如何使用硬编码变量来检查问题是否可以与$ GLOBALS ['HTTP_RAW_POST_DATA']一起使用?

尝试以下方法:

$t1='[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"tid":2,"type":"rpc"}]';
print_r(json_decode($t1));
相关问题