jstree get new json data from tree

captainblack picture captainblack · Dec 23, 2014 · Viewed 20.4k times · Source

I created a tree with the following data. After this process, I made a drag-drop process between menus. And as a result, my menu structure was changed. I want to export new JSON data which has the same structure as my first data. How can I get data from the tree? Please help me.

I tried this code, but this export very complicated JSON. I won't like my first data format.

var v = $('#data').jstree(true).get_json();
var mytext = JSON.stringify(v);
alert(mytext);

First state of menu:

enter image description here

Last state of menu:

enter image description here

// html demo
$('#html').jstree();

// inline data demo



    $(function() {
            var arrayCollection = [
                {"id": "animal", "parent": "#", "text": "Animals"},
                {"id": "device", "parent": "#", "text": "Devices"},
                {"id": "dog", "parent": "animal", "text": "Dogs"},
                {"id": "lion", "parent": "animal", "text": "Lions"},
                {"id": "mobile", "parent": "device", "text": "Mobile Phones"},
                {"id": "lappy", "parent": "device", "text": "Laptops"},
                {"id": "daburman", "parent": "dog", "text": "Dabur Man", "icon": "/"},
                {"id": "Dalmation", "parent": "dog", "text": "Dalmatian", "icon": "/"},
                {"id": "african", "parent": "lion", "text": "African Lion", "icon": "/"},
                {"id": "indian", "parent": "lion", "text": "Indian Lion", "icon": "/"},
                {"id": "apple", "parent": "mobile", "text": "Apple IPhone 6", "icon": "/"},
                {"id": "samsung", "parent": "mobile", "text": "Samsung Note II", "icon": "/"},
                {"id": "lenevo", "parent": "lappy", "text": "Lenevo", "icon": "/"},
                {"id": "hp", "parent": "lappy", "text": "HP", "icon": "/"}
            ];
$('#data').jstree({
    'core' : {
        'check_callback' : true,
        'data' :arrayCollection ,

    },


    "plugins" : ["dnd","wholerow"]
});


});//function

Answer

captainblack picture captainblack · Dec 27, 2014

I found the most simple way to get json from tree;

var v = $('#data').jstree(true).get_json('#', {flat:true})
var mytext = JSON.stringify(v);
alert(mytext);