locked
how to get hierarchical Json Tree data using WebApi RRS feed

  • Question

  • User-1399352090 posted

    how can i create unlimited  children level with  nested tree Json data.

    like 

    [{
     id: 1
     text: 'category1'
     parentid:0,
     children:[{
     id: 1
     text: 'sub1'
     parentid:1,},{id: 100
     text: 'sub2'
     parentid:1,
      children:[]
     }]
    },
    {
     id: 2
     text: 'category2'
     parentid:0,
     children:[{
     id: 101
     text: 'sub3'
     parentid:2,
           children:[
    	   {id:200 
            text: 'sub3a'
            parentid:101,
    	    children:[]},
    	   {
    	    id:201 
            text: 'sub3b'
            parentid:101,
    	    children:[]}
    	   ]
    	}]
    }
    
    {
     id: 3
     text: 'category3'
     parentid:0,
     children:[{
     id: 101
     text: 'sub4'
     parentid:3,
           children:[
    	   {id: 300 
                text: 'sub4a'
                parentid:101,
    	        children:[
    		     {
    	           id:401 
                       text: 'sub4b'
                        parentid:300,
    	            children:[]}
    		]}
    	   
    	   ]
    	}]
    }
    ]

    Appreciate for post any sample code and advices.

    Monday, May 28, 2018 12:03 PM

Answers

  • User36583972 posted


    Hi smd_yasin,

    how can i create unlimited  children level with  nested tree Json data.

    like 

    You can create a HierarchicalNode.

    class Children
    {
        public int id { get; set; }
        public string text { get; set; }
        public string parentid { get; set; }
        public List<Children> children { get; set; }
    }

    Then, you need get the data from the dictionary/database into a hierarchical form.

    Finally, you can use the JsonConverter to serialize the tree to JSON.

    For more detailed, please see: Converting flattened hierarchical data into a tree structured JSON
    https://stackoverflow.com/questions/19929696/converting-flattened-hierarchical-data-into-a-tree-structured-json

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 29, 2018 3:04 AM

All replies

  • User36583972 posted


    Hi smd_yasin,

    how can i create unlimited  children level with  nested tree Json data.

    like 

    You can create a HierarchicalNode.

    class Children
    {
        public int id { get; set; }
        public string text { get; set; }
        public string parentid { get; set; }
        public List<Children> children { get; set; }
    }

    Then, you need get the data from the dictionary/database into a hierarchical form.

    Finally, you can use the JsonConverter to serialize the tree to JSON.

    For more detailed, please see: Converting flattened hierarchical data into a tree structured JSON
    https://stackoverflow.com/questions/19929696/converting-flattened-hierarchical-data-into-a-tree-structured-json

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 29, 2018 3:04 AM
  • User-369506445 posted

    hi

    please try below code

    public class JsonController : ApiController
        {
            // GET: api/Json
            public IHttpActionResult Get()
            {
                Children children = new Children();
                children.id = 1;
                children.text = "category1";
                children.parentid = "0";
                children.children = new List<Children>()
                {
                    new Children(){ id =1 , text ="sub1" , parentid = "1"},
                    new Children(){ id =100 , text ="sub2" , parentid = "1"},
                };
    
                
                return Ok(children);
            }
        }
        class Children
        {
            public int id { get; set; }
            public string text { get; set; }
            public string parentid { get; set; }
            public List<Children> children { get; set; }
        }

    now when you call the API , you get json as same as you want

    Tuesday, May 29, 2018 4:37 AM
  • User-1399352090 posted

    You can create a HierarchicalNode.

    class Children
    {
        public int id { get; set; }
        public string text { get; set; }
        public string parentid { get; set; }
        public List<Children> children { get; set; }
    }

    Then, you need get the data from the dictionary/database into a hierarchical form.

    Thank you 

    Thursday, May 31, 2018 10:22 AM