2

我有这个 JSON 字符串:

 {
    "name": "test task1",
    "desc": "test desc1",
    "id": "1"
}{
    "name": "test task1aaaa",
    "desc": "test desc1",
    "id": "2"
}

但它看起来不正确(JSONLint告诉我)所以 PHPjson_decode()无法解码它。有什么方法可以将两个 JSON 数组分成两个字符串(或数组是多少字符串)以进行json_decode解码?

4

3 回答 3

2

假设您的意图是拥有一个包含两个元素的数组,您的 JSON 应该如下所示:

[
    {
        "name": "test task1",
        "desc": "test desc1",
        "id": "1"
    },{
        "name": "test task1aaaa",
        "desc": "test desc1",
        "id": "2"
    }
]
于 2011-09-15T12:12:58.143 回答
0

最直接的

$str = ' {
    "name": "test task1",
    "desc": "test desc1",
    "id": "1"
}{
    "name": "test task1aaaa",
    "desc": "test desc1",
    "id": "2"
}';

var_dump(json_decode('['.str_replace('}{','},{',$str).']'));
于 2011-09-15T12:15:20.273 回答
0
<?php
$str='{
    "name": "test task1",
    "desc": "test desc1",
    "id": "1"
}{
    "name": "test task1aaaa",
    "desc": "test desc1",
    "id": "2"
}';

$arrays = explode("{", $str);
foreach($arrays as &$arr) $arr='{'.$arr;

//decode
foreach ($arrays as $arr) print_r(json_decode($arr,true));
于 2011-09-15T12:18:30.210 回答