0

I want to iterate JSON data in a tree view structure. For first level I am only able to iterate json value and for second third level failed to iterate the data next next level of data inside the parent array.

The way of declaring my key/value pairs in JSON may also be an issue because I am using the same 'title' keyword.

JSON

{  
           "category":[        
               {  
                 "title":"Customer Satisfaction", // first levl
                 "items":[  
                    {  
                       "title":"Bulletins", // second level
                       "id":"nnanet:category/customer-satisfaction/bulletins"
                    },
                    {  
                       "title":"Consumer Affairs", // second level
                       "id":"nnanet:category/customer-satisfaction/consumer-affairs"
                    },
                    {  
                       "title":"Loyalty",
                       "id":"nnanet:category/customer-satisfaction/loyalty"
                    },
                    {  
                       "title":"Reference Guide",
                       "id":"nnanet:category/customer-satisfaction/reference-guide"
                    },
                    {  
                       "title":"TOI",
                       "id":"nnanet:category/customer-satisfaction/toi"
                    }
                 ]
              },
              {  
                 "title":"Accessories",
                 "id":"nnanet:category/accessories"
              },
              {  
                 "title":"Certified Pre-Owned",
                 "id":"nnanet:category/certified-pre-owned"
              }

           ]
        }

HTML

<div id="all-id">
    <ul class="tree"></ul> // appending html code and json code inside this //
</div>

JavaScript

$.ajax({
    url: '../js/internal/json/documentListing.json',
    datatype: 'json',
    type: 'get',
    cache: 'false',
    success: function(data) {
        $.each(data.category, function(index, value) {
            console.log("Title :" + value.title + "," + "ID :" + value.id);
            var treeViewdata = "<li> <a href='#'>" + items.title + "</a> <ul> <li> <a href='#'>" +
                value.title + "</a> </ul> </li> </li>";
            $(treeViewdata).appendTo(".tree");
        });
    }
});

value.title is the first level of data showing and items.title is the second level of data to iterate. Here I am only getting issue and the condition is failing.

5
  • This is because the category array contains one item with a title "Customer Satisfaction" and that item contains a collection called items. So the bulk of those titles are nested under the first item Commented Aug 22, 2016 at 12:45
  • if i use separate keyword it will work you are saying Commented Aug 22, 2016 at 12:47
  • I guess the question is: should all titles (Bulletins onwards) be children of the Customer Satisfaction title, or should they all be one collection? Noticed the questions been reformatted, makes more sense now Commented Aug 22, 2016 at 12:55
  • this may help: link Commented Aug 22, 2016 at 13:04
  • no tried but not yet resolved Commented Aug 22, 2016 at 13:13

1 Answer 1

0

I used the link I supplied you in the comments along with this: https://stackoverflow.com/a/13347251/4045532.

The key thing is the use of this recursive method:

function json2html(json) {
var i, ret = document.createElement('ul'), li;
for( i in json) {
    li = ret.appendChild(document.createElement('li'));
    li.appendChild(document.createTextNode(i+": "));
    if( typeof json[i] === "object") li.appendChild(json2html(json[i]));
    else li.firstChild.nodeValue += json[i];
}
return ret;

}

The output can be seen here: https://jsfiddle.net/xkL03obg/

It's just one example of how you could achieve this. Hope it helps

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

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.