Recursive entity recursive JSON format for tree structure

Source: Internet
Author: User

Recursive implementation of unlimited menu, product classification, building-style comments, messages and other functions.

The following code cannot be used directly

CREATE TABLE [dbo]. [P_category]  ([Code] [varchar] (*) NOT NULL PRIMARY key,[parent_code] [varchar] (null,[depth] [int] null,[name] [varchar] () NULL) Goinsert [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 101 ', n ' 1 ', 2, n ' wooden doors ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 10101 ', n ' 101 ', 3, n ' Indoor wooden Doors ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 1010101 ', n ' 10101 ', 4, n ' A02 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 101010101 ', n ' 10101 ', 5, n ' A01 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 10101010101 ', n ' 101010101 ', 6, n ' A0101 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 102 ', n ' 1 ', 2, n ' B ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 10201 ', n ' 102 ', 3, n ' B1 ') INSERT [dbo]. [P_category] ([Code], [Parent_code], [Depth], [Name]) VALUES (n ' 1020101 ', n ' 10201 ', 4, n ' B2 ')

Private list<category> categorycachealllist {get; set;} [Route ("")]public httpresponsemessage Get () {var list = Cachehelper<list<category>>. GetCache (Categoryalllistcachekey), if (list = = null) {categorycachealllist = Categoryservice.getcachelist (); Get all data inside the database List = new list<category> (); Categoryjson (List, "1"); Cachehelper<list<category>>. Setcache (Categoryalllistcachekey, list);} Return Request.createresponse (Httpstatuscode.ok, list); The following code does not try//string json = jsonconvert.serializeobject (categorylist, formatting.indented);//return json;} <summary>///Get Brother Node///</summary>///<param name= "CategoryList" ></param>///<param Name= "Parentcode" ></param>public void Categoryjson (list<category> categorylist, string parentCode) { var list = Categorycachealllist.findall (p = = P.parentcode = = Parentcode), if (list. Count > 0) {foreach (var item in list) {Categorytreejson (item, item. Code); Categorylist.add (item);}}} &ltsummary>///Recursive sub-object///</summary>///<param name= "sbcategory" ></param>///<param name= " Parentcode "></param>private void Categorytreejson (Category sbcategory, string parentcode) {var list = Categorycachealllist.findall (p = = P.parentcode = = Parentcode), if (list. Count > 0) {sbcategory.children = new list<category> (); foreach (var item in List) {Categorytreejson (item, item. Code); SbCategory.Children.Add (item);}}}

  

Recursive entity recursive JSON format for tree structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.