如何在PHP中解码json数组?

时间:2013-10-04 06:21:40

标签: php

假设我有一个类似

的json数组
{
    [
       {"id" : 1, "src" : src1, "name" : name1},
       {"id" : 2, "src" : src2, "name" : name2},
       {"id" : 3, "src" : src3, "name" : name3},
       {"id" : 4, "src" : src4, "name" : name4}
    ]
}

我应该如何在PHP中解码这个json数组?

$project_info_json = $_POST['project_info_json'];

if (get_magic_quotes_gpc()) {
    $project_info_json =  stripslashes($_POST['project_info_json']);
}else {
    $project_info_json = $_POST['project_info_json'];
}
$project_info_array = json_decode($project_info_json, true);

我已经尝试了json_decode方法,但似乎$ project_info_array有0个元素计数。任何人都可以告诉我,我做错了什么?

2 个答案:

答案 0 :(得分:0)

我认为这不是有效的JSON

JSON应该像

[
    {
        "id": 1,
        "src": "src1",
        "name": "name1"
    },
    {
        "id": 2,
        "src": "src2",
        "name": "name2"
    },
    {
        "id": 3,
        "src": "src3",
        "name": "name3"
    },
    {
        "id": 4,
        "src": "src4",
        "name": "name4"
    }
]

验证您的JSON @ http://jsonlint.com/

答案 1 :(得分:-1)

您在json中的外部对象没有存储内部列表的键。

此外,应引用json中的字符串。 src1name1不加引号。