title: Save ul list into json object
body: <p>I'm trying to save a list into <code>json</code> object by looping through it using a recursive function, so this is what I did so far:<br>
  <strong>My list</strong>:</p>
  
  <pre><code>&lt;ul id="Menu"&gt;
      &lt;li&gt;Menu
          &lt;ul&gt;
              &lt;li&gt;Test 1&lt;/li&gt;
              &lt;li&gt;Test 2
                  &lt;ul&gt;
                      &lt;li&gt;Sub test 2/1&lt;/li&gt;
                      &lt;li&gt;Sub test 2/2&lt;/li&gt;
                      &lt;li&gt;Sub test 2/3&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;Test 3&lt;/li&gt;
              &lt;li&gt;Test 4&lt;/li&gt;     
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Menu2&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>My script:</strong><br></p>
  
  <pre><code>&lt;script&gt;
  jQuery(document).ready(function(){
      item = {};
      buildMenu($('#Menu'), item);
      menu.push(item);
      console.log(JSON.stringify(menu));  
  });
  var menu = [];
  
  function buildMenu(ul, item){
      var children = [];
      ul.find('li').each(function(){
          var current = $(this);
          if(current.find('ul').length &gt; 0){
              var newItem = {};
              buildMenu(current.find('ul'), newItem);
              children.push(newItem);
          }else{
              children.push(current.text());
          }
      });
  
      var txt = $(this).text();
      console.log('pushing into: '+txt);
      item[txt] = children;   
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>I want to save each menu and it's children inside it as json objects and send it to php but I'm having wrong output:</p>
  
  <pre><code>[{"MenuTest 1Test 2Sub test 2/1Sub test 2/2Sub test 2/3Test 3Test 4Menu2":[{"Test 1Test 2Sub test 2/1Sub test 2/2Sub test 2/3Test 3Test 4":["Test 1",{"Sub test 2/1Sub test 2/2Sub test 2/3":["Sub test 2/1","Sub test 2/2","Sub test 2/3"]},"Sub test 2/1","Sub test 2/2","Sub test 2/3","Test 3","Test 4"]},"Test 1",{"Sub test 2/1Sub test 2/2Sub test 2/3":["Sub test 2/1","Sub test 2/2","Sub test 2/3"]},"Sub test 2/1","Sub test 2/2","Sub test 2/3","Test 3","Test 4","Menu2"]}]
  </code></pre>
  
  <p>What's wrong with my function ?</p>