douyalin0847 2011-08-30 18:52
浏览 43

需要帮助解决JSON问题

i'm with this problem at PHP when i'm try to convert an array to an json I have an recursive function that build the array to encode it on jSon format.

That's the array:

$data = array(
    array('id' => 1, 'parent_id' => null, 'text' => 'lorem ipsum'),
    array('id' => 2, 'parent_id' => 1, 'text' => 'lorem ipsum1'),
    array('id' => 3, 'parent_id' => 1, 'text' => 'lorem ipsum2'),
    array('id' => 4, 'parent_id' => 2, 'text' => 'lorem ipsum3'),
    array('id' => 5, 'parent_id' => 3, 'text' => 'lorem ipsum4'),
    array('id' => 6, 'parent_id' => null, 'text' => 'lorem ipsum5'),
);

Thats my function:

function trataArray($data) {


$itemsByReference = array();

// Build array of item references:
foreach ($data as $key => &$item) {
    $itemsByReference[$item['id']] = &$item;
    // Children array:
    $itemsByReference[$item['id']]['children'] = array();
    // Empty data class (so that json_encode adds "data: {}" )
    $itemsByReference[$item['id']]['data'] = new StdClass();
}

// Set items as children of the relevant parent item.
foreach ($data as $key => &$item)
    if ($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
        $itemsByReference [$item['parent_id']]['children'][] = &$item;

// Remove items that were added to parents elsewhere:
foreach ($data as $key => &$item) {
    if ($item['parent_id'] && isset($itemsByReference[$item['parent_id']]))
        unset($data[$key]);
}

// Encode:
return $data;

}

Thats my json, but this json have errors:

{
    "0": // Problem here... T_T
     {
        "id": 1,
        "parent_id": null,
        "name": "lorem ipsum",
        "children": [
            {
                "id": 2,
                "parent_id": 1,
                "name": "lorem ipsum1",
                "children": [
                    {
                        "id": 4,
                        "parent_id": 2,
                        "name": "lorem ipsum3",
                        "children": [],
                        "data": {}
                    }
                ],
                "data": {}
            },
            {
                "id": 3,
                "parent_id": 1,
                "name": "lorem ipsum2",
                "children": [
                    {
                        "id": 5,
                        "parent_id": 3,
                        "name": "lorem ipsum4",
                        "children": [],
                        "data": {}
                    }
                ],
                "data": {}
            }
        ],
        "data": {}
    },
    "5":  // And here... T_T
    {
        "id": 6,
        "parent_id": null,
        "name": "lorem ipsum5",
        "children": [],
        "data": {}
    }
}

The strings "0": and "5": are causing an error on json markup. i thinked to try to remove it using preg_replace but i'm terrible working with regular expressions. Someone could help me with it... :D

Thanks guys!


I had an idea... :)

I was looking for ways to test thins shit... :D And i come with an idea, get the JSON from the plugin and ENCODE it as one array and print it to see what i would need to give to JSON Encode to give me the correct thing.

Them he reproduce it:

    Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [text] => City
            [children] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 11
                            [text] => Wyoming
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 111
                                            [text] => Albin
                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => 112
                                            [text] => Canon
                                        )

                                    [2] => stdClass Object
                                        (
                                            [id] => 113
                                            [text] => Egbert
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 12
                            [text] => Washington
                            [state] => closed
                            [children] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 121
                                            [text] => Bellingham
                                        )

                                    [1] => stdClass Object
                                        (
                                            [id] => 122
                                            [text] => Chehalis
                                        )

                                    [2] => stdClass Object
                                        (
                                            [id] => 123
                                            [text] => Ellensburg
                                        )

                                    [3] => stdClass Object
                                        (
                                            [id] => 124
                                            [text] => Monroe
                                        )

                                )

                        )

                )

        )

)

The JSON its base on OBJECTS, i think the bug its there, im tryng to work with arrays and convert it, when the data its an object.

Someone knows how to create something like it? Just an way to i can think... Thanks guys...

:DDD

  • 写回答

4条回答 默认 最新

  • dongpa2000 2011-08-30 18:58
    关注

    According to jslint.com, this is valid JSON. It passes the validator just fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题