title: How to convert a hierarchy list to json
body: <p>I am using ASP.NET MVC3</p>
  
  <p>I have a hierarchy list with this type:</p>
  
  <pre><code>public class TreeNode
  {
  public int id;
  public int title;
  public int parentid;
  }
  var myHierarchyList=new List&lt;TreeNode&gt;();
  //(the tree has a root with id=1 and parentid=0 and title=root)
  </code></pre>
  
  <p>I need a function to generate json data from myHierarchyList. somthing like this:</p>
  
  <pre><code>[{
      "id":1,
      "text":"cat1",
      "childrens":[{
          "id":2,
          "text":"cat2"
      },{
          "id":3,
          "text":"cat3",
          "childrens":[{
              "id":4,
              "text":"cat4"
          },{
              "id": 8,
              "text":"cat5"
          }]
      }]
  }]
  </code></pre>
  
  <p>Please help me.
  Thanks.</p>