php json_decode,数据由[]包装

时间:2013-08-01 01:08:49

标签: php json

使用true选项,它可以正常工作。

抱歉,谢谢你们。

=============================================

我使用此代码将php数组编码为json

$rows = array();
if ($result = $mysqli->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    echo json_encode($rows);
    /* free result set */
    $result->free();
}

结束解码
$array = json_decode($server_output)

$ server_output就像这样

[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}]
[{"userid":"153795","userinfor":"xxxxxxxxx","userlocation":"CA"}]
[{"userid":"131878","userinfor":"xxxxxxxxx","userlocation":"CA"}]

但是,$ array为NULL :(

感谢advence,

1 个答案:

答案 0 :(得分:0)

这在一起是无效的json:

[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}]
[{"userid":"153795","userinfor":"xxxxxxxxx","userlocation":"CA"}]
[{"userid":"131878","userinfor":"xxxxxxxxx","userlocation":"CA"}]

虽然,这部分是有效的:

[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}]

或其中任何一个单独

$json = '[{"userid":"96679","userinfor":"xxxxxxxxx","userlocation":"CA"}]';
print_r(json_decode($json, true));

输出:

Array
(
    [0] => Array
        (
            [userid] => 96679
            [userinfor] => xxxxxxxxx
            [userlocation] => CA
        )

)

所有数据的有效格式如下:

[
    {
        "userid": "96679",
        "userinfor": "xxxxxxxxx",
        "userlocation": "CA"
    },
    {
        "userid": "153795",
        "userinfor": "xxxxxxxxx",
        "userlocation": "CA"
    },
    {
        "userid": "131878",
        "userinfor": "xxxxxxxxx",
        "userlocation": "CA"
    }
]