1. Define a type First
Public classNode {[Jsonproperty (PropertyName="ID", nullvaluehandling =Nullvaluehandling.ignore)] Public stringID {Get;Set; } [Jsonproperty (PropertyName="text", nullvaluehandling =Nullvaluehandling.ignore)] Public stringText {Get;Set; } [Jsonproperty (PropertyName="checked", nullvaluehandling =Nullvaluehandling.ignore)] Public BOOLChecked {Get;Set; } [Jsonproperty (PropertyName="Children", nullvaluehandling =Nullvaluehandling.ignore)] PublicIlist<node> Children {Get;Set; } [Jsonproperty (PropertyName="ParentID", nullvaluehandling =Nullvaluehandling.ignore)] Public stringParentID {Get;Set; } }
2. Data sources, we find out from the database is generally the following data
Ilist<node> treelist =NewList<node>();
Treelist.add (NewNode {Id ="f31a347e4be70da6d925bfaddf0e896b", Text ="Business Classics", ParentID ="0"}); Treelist.add (NewNode {Id ="b50d381e694c0227242ff7b55685178c", Text ="LZ01", ParentID ="f31a347e4be70da6d925bfaddf0e896b"}); Treelist.add (NewNode {Id ="a921c809276dadf00bd45dd527564f02", Text ="Leisure Time", ParentID ="0"}); Treelist.add (NewNode {Id ="1BF52435E4F0AF478DC9137CA7719FBB", Text ="XX01", ParentID ="a921c809276dadf00bd45dd527564f02"}); Treelist.add (NewNode {Id ="Ee43cc1ceb57a3793c5c11d6d632fd22", Text ="Modern Times", ParentID ="0"}); Treelist.add (NewNode {Id ="Fb9a268c6061d962dbb5fc5f55c803f8", Text ="MD01", ParentID ="Ee43cc1ceb57a3793c5c11d6d632fd22"});
3. Recursive initialization tree
// <summary> ///Recursive initialization tree/// </summary> /// <param name= "Nodes" >Results</param> /// <param name= "ParentID" >Parent ID</param> /// <param name= "Sources" >Data Source</param> Private voidInittree (ilist<node> nodes,stringParentID, ilist<node>sources) {Node Tempnode; //recursive search for child nodes varTemptree = sources. Where (item = Item. ParentID = =parentid). ToList (); foreach(Node rowinchTemptree) {Tempnode=NewNode () {Id=row. Id, Text=row. Text, ParentID=row. ParentID, children=NewList<node>() }; Nodes. ADD (Tempnode); Inittree (Tempnode.children, row. Id, sources); } }
4. Call to get results
varTree =NewList<node>(); Inittree (Tree,"0", treelist);stringJSON =jsonconvert.serializeobject (tree);
//results obtained: [{"id": "f31a347e4be70da6d925bfaddf0e896b", "text": "Business classic", "checked": false, "children": [{"id": " B50d381e694c0227242ff7b55685178c "," text ":" LZ01 "," checked ": false," children ": []," ParentID ":" f31a347e4be70da6d925bfaddf0e896b "}]," ParentID ":" 0 "},{" id ":" a921c809276dadf00bd45dd527564f02 "," text ":" Leisure Time "," Checked ": false," children ": [{" id ":" 1BF52435E4F0AF478DC9137CA7719FBB "," text ":" XX01 "," checked ": false," children ": [] , "ParentID": "A921c809276dadf00bd45dd527564f02"}], "ParentID": "0"},{"id": "ee43cc1ceb57a3793c5c11d6d632fd22", " Text ":" Modern Times "," checked ": false," children ": [{" id ":" fb9a268c6061d962dbb5fc5f55c803f8 "," text ":" MD01 "," checked ": False, "children": [], "ParentID": "Ee43cc1ceb57a3793c5c11d6d632fd22"}], "ParentID": "0"}]
C # implements tree, containing parentid and children