简体   繁体   中英

about the json format (jstree, php)

I am trying to make the data format like below. for using jstree module

[{
    "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"
        }]
    }]
}]

but.. mine is

[{
    "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"
}]

I have no idea about the arrays or JSON. Here is the code:

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

Can anyone help me?

  1. You are mixing different methods to form json for jsTree. You either 1) use a multilevel structure using key children - you have that in the beginning of your result json, or 2) you use a one-level structure, don't use children keyword, just specify the parent node. I suggest you use the second way.

  2. Instead of parent_id you should use simply parent .

  3. Your output json contains duplicate entries with ids '1', '3', '9'. It is no good.

  4. For root node use # as id.

To me your target json should look like below. To get it the correct php iteration snippet would probably look closer to as below, but you will have to specifically filter out duplicate entries which is not in the code yet.

Then you will get the expected result as in this demo - 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;        
}

Correct 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"
}]

Also check docs at jsTree json reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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