1

JSON:

{
  "Item 1": [
    9,
    {
      "Item 1.1" : 19,
      "Item 1.2" : 29,
      "Item 1.3" : 39
    }
  ],
  "Item 2": 49,
  "Item 3": [
    59,
    {
      "Item 3.1" : 69,
      "Item 3.2" : 79,
      "Item 3.3" : 89
    }
  ]
}

Desired HTML:

<ul class="list">
    <li class="item">
        <a data-page="9">Item 1</a>
        <ul>
            <li><a data-page="19">Item 1.1</a></li>
            <li><a data-page="29">Item 1.2</a></li>
            <li><a data-page="39">Item 1.3</a></li>
        </ul>
    </li>
    <li class="item"><a data-page="49">Item 2</a></li>
    <li class="item">
        <a data-page="59">Item 3</a>
        <ul>
            <li><a data-page="69">Item 3.1</a></li>
            <li><a data-page="79">Item 3.2</a></li>
            <li><a data-page="89">Item 3.3</a></li>
        </ul>
    </li>
</ul>

My attempt (http://jsbin.com/duxoyitifa/edit?js,console,output):

$(document).ready(function() {

  var json = '{"Item 1":[9,{"Item 1.1":19,"Item 1.2":29,"Item 1.3":39}],"Item 2":49,"Item 3":[59,{"Item 3.1":69,"Item 3.2":79,"Item 3.3":89}]}';

  var obj = $.parseJSON( json );
  //console.log(obj);  
  var items = [];

  $.each( obj, function( k1, v1 ) {
    console.log("k1: " + k1);
    console.log(v1);
    items.push('<li class="item">');

    $.each( v1, function( k2, v2 ) {
      console.log(v2);
      items.push('<a data-page="'+v2+'">'+k1+'</a>');

      $.each (v2, function( k3, v3 ) {
        console.log(v3);
      });

    });
    items.push('</li>');
    //items.push('<li class="item">');
    //items.push('<a data-page="'+n+'">'+key+'</a>');
    //items.push('</li>');

    $("body").append(items.join( "" ));

  });

});

I can't figure how to get the n and the sub-list from inside the nested object.

2 Answers 2

3

$(document).ready(function() {

  var json = '{"Item 1":[9,{"Item 1.1":19,"Item 1.2":29,"Item 1.3":39}],"Item 2":49,"Item 3":[59,{"Item 3.1":69,"Item 3.2":79,"Item 3.3":89}]}';

  var obj = $.parseJSON(json);
  //console.log(obj);  
  var items = [];

  function eachObj(obj) {
    $.each(obj, function(key, value) {
      console.log("iteration start");
      console.log(key, value);
      console.log("iteration end");

      items.push('<li class="item">');
      if (value != null && Array.isArray(value)) {
        items.push('<a data-page="' + value[0] + '">' + key + '</a>');
        items.push('<ul>');
        eachObj(value[1]);
        items.push('</ul>');
      } else {
        items.push('<a data-page="' + value + '">' + key + '</a>');
      }
      items.push('</li>');
    });
  }

  eachObj(obj);

  $('ul.list').html(items.join(""));

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="list">
</ul>

Sign up to request clarification or add additional context in comments.

3 Comments

@3zzy Remember that function(data, status) is a callback so you'll need to kick off the each loop there.
One minor issue: the nested <li> should not have the item class, how do I check that?
Yes I can do if (Array.isArray(value)) but there are some items without sub-items so those are affected
0

A recursive function would be would be needed.

(some pseudocode while I figure out an exact solution)

function parseForm(json){
  var string = "";
  function parseElement(j){
    if(j.constructor === Object){
      //write element to "string"
      for(var i=0; i<j.length; i++){
        parseForm(j[i]);
      }
      //write end tag
    }else if(j.constructor === Array){
      //write element
      for(var i in j){
        parseForm(j[i]);
      }
      //write end tag
    }else{
      //write
    }
  }

  parseForm(json);

  return string;
}

This goes through at infinite depth. The key to this solution is sandwiching the recursive part in between the start/end tags. I do not know exactly what the rules are for formatting; I will let you figure out the specific details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.