javascript - How to convert an array tree to Json tree? -


i have array keep data in tree structure i'd transform data json. example of data :

[object]    0: object       children: array[1]           0: object              children: array[1]                 0: object                    children: array[10]                       0: object                          children: array[6]                          value: object                       1: object                       2: object                       3: object                       4: object                       5: object                       6: object                       7: object                       8: object                       9: object                       value: object                          id: "00145e5bb2641ee284f811a7907757a3"                          parent: "00145e5bb2641ee284f811a7907737a3"                          ref: undefined                          text: "functional areas"                          type: "twb" 

now want transform these array of data json. tried json.stringify(myarray); format isn't correct. destroys structure of tree. ideas?

this code give result above.

function transformtostruct(xmldata, callback) {     var data = xmldata.item;             var roots=[];     var children = {};     var arry=createstructure(data);     // find top level nodes , hash children based on parent   (var = 0, len = arry.length; < len; ++i) {       var item = arry[i],           p = item.parent,           target = [];          if(p == rootid) {             target = roots;         }          else {                           target = (children[p] || (children[p] = []));                    }          target.push({ value: item });   }   // function recursively build tree   var findchildren = function(parent) {       if (children[parent.value.id]) {           parent.children = children[parent.value.id];           (var = 0, len = parent.children.length; < len; ++i) {               findchildren(parent.children[i]);           }       }   };    // enumerate through handle case there multiple roots   (var = 0, len = roots.length; < len; ++i) {       findchildren(roots[i]);   } //  var returnv = json.stringify(roots); //  callback(returnv);   callback(roots) }  function createstructure(data) {     var arry = [];     $.each(data, function(i, val) {         var parent = val["parentid"]["#text"];         var id = val["nodeid"]["#text"];         var text = val["nodetext"]["#text"];         var level = val["nodelevel"]["#text"];         var ref = val["refobject"]["#text"];         var type = (val["nodetype"]["#text"]).substring(0,3);         if(level == "01")             rootid = parent;         var item = {"id": id, "text": text, "parent": parent, "type" : type, "ref" : ref};         arry[i] = item; //      arry.push(item);             });     console.log(arry)     return arry; } 

json can encode ("stringify") native javascript object in string notation. can reversely create other instance of object notation.

sample:

var myarray = new array("a", "b", "c"); var jsonnotation = json.stringify(myarray); console.log(jsonnotation); 

will give:

[  "a",  "b",  "c" ] 

the code above

[object]    0: object       children: array[1] 

is not javascript.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -