需要使用JSON问题的帮助 [英] Needing help with JSON problems

查看:102
本文介绍了需要使用JSON问题的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是与PHP这个问题,当我试图将数组转换成JSON
我有一个建立数组EN code它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.

这就是数组:

$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;

}

这就是我的JSON,但是这个JSON有错误:

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": {}
    }
}

字符串0:和5:正在造成对JSON标记错误。
我鑫卡特尝试使用preg_replace但我可怕定期EX pressions工作它删除。有人可以帮我一下吧......:D

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

谢谢你们!

我有一个想法...:)

I had an idea... :)

我一直在寻找方法来测试变薄狗屎...:D
和我带着一个想法,从插件和EN code将其作为一个阵列得到JSON和打印,看看有什么我需要给JSON恩code给我正确的事情。

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.

他们,他重现:

    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
                                        )

                                )

                        )

                )

        )

)

在其JSON对象上的基础,我认为错误的出现,即时通讯tryng使用数组的工作,并将其转换,当数据的一个对象。

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

推荐答案

你是如何看到的错误?

简单直接粘贴您的JSON到<脚本> 模块和加载在浏览器中提出无论是在IE和Chrome的错误,但是,分配相同的JSON到一个变量第一prevents任何错误消息。该关闭编译器证实了这些结果;当你裸露的JSON通过编译运行,它失败。当引入一个简单的变量赋值,一切编译罚款。

Simply pasting your JSON directly into <script> blocks and loading that in a browser raises errors both in IE and Chrome, however, assigning the same JSON to a variable first prevents any error messages. The Closure Compiler confirms these findings; when your bare JSON is run through compilation, it fails. When a simple variable assignment is introduced, everything compiles fine.

似乎有JS的一些词​​法规则,我是不知道的是影响本;可悲的是,我不知道的根本原因到底是什么。

It seems there are some lexical rules of JS that I'm not aware of that is affecting this; sadly, I'm not sure exactly what the root cause is.

另外,我不知道你究竟是如何产生的最终JSON。我的意思是,我知道你正在使用 json_en code(),但无论怎样我的数组键做,我不能重现你的输出的使用PHP中的数组

Also, I'm not sure how exactly you're producing the resultant JSON. I mean, I know you're using json_encode(), but no matter what I do with the array key, I cannot reproduce your output using an array in PHP.

我期待模仿这种

{"0": "foo"}

然而,在PHP中使用数字数组键将意味着最终JSON将是一个数组;例如:

However, using numeric array keys in PHP will mean that the resultant JSON will be an array; for example:

json_encode( array(0 => 'foo') );
> (string) ["foo"]
json_encode( array("0" => 'foo') );
> (string) ["foo"]

这是我可以重新创建你的JSON的唯一方法是通过执行以下操作:

The ONLY way that I can recreate your JSON is by doing the following:

$x = new stdClass;
$x->{"0"} = 'foo';
json_encode($x);
> (string) {"0": "foo"}

所以,我想无论是你的榜样是不完整/误导/错误的,或者你已经发现了在PHP中一些非常不起眼的错误。

So, I'm thinking either your example is incomplete/misleading/wrong, or you've uncovered some extremely obscure bug in PHP.

介绍空格键,其他排列像下面这样,也不会产生你的JSON:

Introducing spaces to the key, and other permutations like the following, also does not produce your JSON:

json_encode( array("0 " => 'foo') );
> (string) {"0 ": "foo"}

json_encode( array(" 0" => 'foo') );
> (string) {" 0": "foo"}

json_encode( array("00" => 'foo') );
> (string) {"00": "foo"}

json_encode( array(false => 'foo') );
> (string) ["foo"]

json_encode( array(null => 'foo') );
> (string) {"": "foo"}

json_encode( array("" => 'foo') );
> (string) {"": "foo"}

所以,不知道发生了什么事。

So, not sure what's going on.

这篇关于需要使用JSON问题的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆