【问题标题】:about the json format (jstree, php)关于 json 格式(jstree、php)
【发布时间】:2016-02-03 09:48:00
【问题描述】:

我正在尝试制作如下所示的数据格式。使用jstree模块

[{
    "id":"1" ,
    "parent_id":"0",
    "text":"TEST",
    "children":[{
        "id":"2",
        "parent_id":"1",
        "text":"TEST1",
        "children":[{
            "id":"4",
            "parent_id":"2",
            "text":"TEST1-1"
        }, {
            "id":"5",
            "parent_id":"2",
            "text":"TEST1-2"        
        }, {
            "id":"6",
            "parent_id":"2",
            "text":"TEST1-3"        
        }]
    },  {
        "id":"3",
        "parent_id":"1",
        "text":"TEST2",
        "children":[{
            "id":"7",
            "parent_id":"3",
            "text":"TEST2-1"
        }, {
            "id":"8",
            "parent_id":"3",
            "text":"TEST2-1"
        }, {
            "id":"9",
            "parent_id":"3",
            "text":"TEST2-1"
        }]
    }]
}]

但是..我的是

[{
    "id":"1" ,
    "parent_id":"0",
    "text":"TEST",
    "children":{
        "id":"3",
        "parent_id":"1",
        "text":"\ud14c\uc2a4\ud2b82",
        "children":{
            "id":"9",
            "parent_id":"3",
            "text":"\ud14c\uc2a4\ud2b82-3"
        }
    }
},{
    "children":{
        "id":"1",
        "parent_id":"0",
        "text":"TEST1",
        "children":{
            "id":"3",
            "parent_id":"1",
            "text":"TEST2",
            "children":{
                "id":"9",
                "parent_id":"3",
                "text":"TEST2-3"
            }
        }
    }
},{
    "id":"2",
    "parent_id":"1",
    "text":"TEST1",
    "children":{
        "id":"6",
        "parent_id":"2",
        "text":"TEST1-3"
    }
},{
    "id":"3",
    "parent_id":"1",
    "text":"TEST2",
    "children":{
        "id":"9",
        "parent_id":"3",
        "text":"TEST2-3"
    }
},{
    "id":"4",
    "parent_id":"2",
    "text":"TEST1-1"
},{
    "id":"5",
    "parent_id":"2",
    "text":"TEST1-2"
},{
    "id":"6",
    "parent_id":"2",
    "text":"TEST1-3"
},{
    "id":"7",
    "parent_id":"3",
    "text":"TEST2-1"
},{
    "id":"8",
    "parent_id":"3",
    "text":"TEST2-2"
},{
    "id":"9",
    "parent_id":"3",
    "text":"TEST2-3"
}]

我不知道数组或 JSON。代码如下:

$refs = array();
$list = array();

$sql = "
select id, parent_id, text
from code
where use_yn = 'Y'
$result = mysql_query($sql);
while($data = mysql_fetch_assoc($result)) {
    $thisref = &$refs[$data['id']];
    $thisref['id'] = $data['id'];
    $thisref['parent_id'] = $data['parent_id'];
    $thisref['text'] = iconv("euc-kr", "utf-8", $data['text']);

    if ($data['id'] == 0) {
        $list[ $data['id'] ] = &$thisref;
    } else {
        $refs[ $data['parent_id'] ]['children'] = &$thisref;
    }
}

echo json_encode(array_values($refs));

谁能帮帮我?

【问题讨论】:

  • 现在我已经格式化了您的代码,您可以看到包含 SQL 查询的字符串格式错误。

标签: php jquery arrays json jstree


【解决方案1】:
  1. 您正在混合不同的方法来为 jsTree 形成 json。 您要么 1) 使用键 children 使用多级结构 - 您在结果 json 的开头有它,或者 2) 您使用单级结构,不要使用 children 关键字,只需指定父节点.我建议你使用第二种方式。

  2. 您应该简单地使用parent,而不是parent_id

  3. 您的输出 json 包含 ID 为“1”、“3”、“9”的重复条目。这不好。

  4. 对于根节点,使用 # 作为 id。

对我来说,您的目标 json 应该如下所示。为了得到它正确的 php 迭代 sn-p 可能看起来更接近如下,但您必须专门过滤掉代码中尚未出现的重复条目。

然后您将获得本演示中的预期结果 - Fiddle

while($data = mysql_fetch_assoc($result)) {
    $thisref = &$refs[$data['id']];
    $thisref['id'] = $data['id'] == 0 ? '#' : $data['id']; // if id is 0  , it's a root node, replace with '#'
    $thisref['parent'] = $data['parent_id'] == 0 ? '#' : $data['parent_id'];// if parent id is 0, it's a root node, replace with '#'
    $thisref['text'] = iconv("euc-kr", "utf-8", $data['text']);

    $refs[] = &$thisref;        
}

正确的json:

var data = [{
    "id": "1",
    "parent": "#",
    "text": "TEST"
}, {
    "id": "3",
    "parent": "1",
    "text": "테스트2"
}, {
    "id": "9",
    "parent": "3",
    "text": "테스트2-3"
}, {
    "id": "2",
    "parent": "1",
    "text": "TEST1"
}, {
    "id": "6",
    "parent": "2",
    "text": "TEST1-3"
}, {
    "id": "4",
    "parent": "2",
    "text": "TEST1-1"
}, {
    "id": "5",
    "parent": "2",
    "text": "TEST1-2"
}, {
    "id": "6",
    "parent": "2",
    "text": "TEST1-3"
}, {
    "id": "7",
    "parent": "3",
    "text": "TEST2-1"
}, {
    "id": "8",
    "parent": "3",
    "text": "TEST2-2"
}]

也可以在jsTree json reference查看文档

【讨论】:

  • 我试过你写的 PHP 代码,但我的输出看起来一样.. [{"id":"9","parent":"3","text":"\ud14c\ uc2a4\ud2b82-3"} ...... }] 所以我改变了 1.$thisref['id'] = $data['id'] == 1 ? '#' : $data['id']; $thisref['id'] = $data['id']; 2.$refs[] = &$thisref;$refs[$data['id']] = &$thisref; 最后它工作了!!!谢谢!!尼古拉!
  • 很高兴我能帮上忙!然后您能否通过单击左侧的复选标记来接受答案。
猜你喜欢
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多