简体   繁体   English

如何在php中解码json数组?

[英]how to decode json array in php?

lets say I have an json array like 可以说我有一个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}
    ]
}

how should I decode this json array in php?? 我应该如何在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);

I already tried json_decode method but it seems like $project_info_array has 0 element count. 我已经尝试过json_decode方法,但似乎$ project_info_array的元素计数为0。 can anybody tell me what am I doing wrong? 有人可以告诉我我在做什么错吗?

i Think this is not valid JSON 我认为这是无效的JSON

JSON should be like 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"
    }
]

Validate Your JSON @ http://jsonlint.com/ 验证JSON @ http://jsonlint.com/

Your outer object in json does not have a key where the internal list is stored in. json中的外部对象没有内部列表存储所在的键。

Also, your strings in json should be quoted. 另外,您在json中的字符串应加引号。 src1 , name1 are unquoted. src1name1未引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM