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.