0

我想解码 json 字符串,包括 PHP 中的数组和对象。当我解码时


$array = json_decode($json, true); 
print_r($array);

它返回 NULL。让我知道,在 PHP 中解码 json 的方法。这是我的 json 字符串。


{
    success: 1,
    message: "Successful!",
    save_date: "2013-09-11 04:09:26",
    test: [
        {
        test_id: "1",
        test_date: "2013-09-12",
        test_name: "Test 1"
        },
        {
        test_id: "2",
        test_date: "2013-09-11",
        test_name: "Test 2"
        }
    ]
}

4

4 回答 4

0

您的 JSON 无效,属性名称也需要用引号引起来。

像这样:

{
    "success": 1,
    "message": "Successful!",
    "save_date": "2013-09-11 04:09:26",
    "test": []
}

提示:使用JSONLint验证您的 JSON。

于 2013-09-12T08:31:02.440 回答
0

这不是一个有效的 JSON 对象。JSON 对象必须用双引号将所有属性名称括起来:

{ "success": 1, "message": "Successful!" }

PHP 提供了方便的json_last_error_msg函数来告诉您这一点。

还有用于验证 JSON 字符串的在线工具JSONLint 。

于 2013-09-12T08:30:07.240 回答
0

您的 json 字符串应如下所示:

$sJson = '{"success": 1,"message": "Successful!","save_date": "2013-09-11 04:09:26",
           "test": [    {"test_id": "1","test_date": "2013-09-12","test_name": "Test 1"},
                {"test_id": "2","test_date": "2013-09-11","test_name": "Test 2"}]}';
于 2013-09-12T08:42:30.367 回答
-1

用这个

   $result=(array)json_decode('your json string');

我认为它对你有用

于 2013-09-12T08:30:46.903 回答