从$ _POST收到的json读取关联数组

时间:2013-02-25 04:14:22

标签: php json post

我有一个php文件接收$ _GET信息和$ _POST信息,对于$ _GET信息没问题,所以对于我收到此字符串的$ _POST信息:

[{"channel":"\/meta\/handshake","id":"10","minimumVersion":"1.0","supportedConnectionTypes":["websocket","long-polling"],"version":"1.0"}]

以[开始]结束。

所以我怎么读这个?谢谢你的帮助!

3 个答案:

答案 0 :(得分:1)

首先解码像这样的json数据

$arr = json_decode($_POST,true);
然后

访问这样的数据

echo $arr[0]['channel']; // output "/meta/handshake"

工作示例http://codepad.viper-7.com/ZeI9n3

答案 1 :(得分:1)

您可以使用json_decode` function

$channels = json_decode(file_get_contents('php://input')); // parse raw post
print_r($channels) // print structure of channels

答案 2 :(得分:0)

试试这个:

$str  = '[{"channel":"\/meta\/handshake","id":"10","minimumVersion":"1.0","supportedConnectionTypes":["websocket","long-polling"],"version":"1.0"}]';


echo "<pre>";
$arra   = json_decode($str,true);
print_r($arra);
/*Uncomment this for your out put*/
//echo "Required : ".echo $arra[0]['channel']; 

输出:

Array
(
    [0] => Array
        (
            [channel] => /meta/handshake
            [id] => 10
            [minimumVersion] => 1.0
            [supportedConnectionTypes] => Array
                (
                    [0] => websocket
                    [1] => long-polling
                )

            [version] => 1.0
        )

)

参考:http://php.net/manual/en/function.json-decode.php