JSON: How to access deep level array -
everything pre-loop fine. need finding way access deeper level json. i'm stuck in "for" loop. product_name comes out okay in first loop nothing deeper that. i've added output each deeper level loop don't seem pass second one.
follow url view json array.
<script> var xmlhttp = new xmlhttprequest(); var url = 'http://www.weber.se/?type=88&pagealias=lecareglttklinker26&json=1'; xmlhttp.onreadystatechange=function() { if (this.readystate == 4 && this.status == 200) { readjson(this.responsetext); } } xmlhttp.open("get", url, true); xmlhttp.send(); function readjson(response) { var object = json.parse(response); var output = '<div class="container-fluid">'; output += '<div class="row">'; output += '<div class="col-xs-6 text-left"><img src="' + object.product[0].packaging_picture + '" height="230"></div>'; output += '<div class="col-xs-6 text-right"><img src="' + object.product[0].main_picture + '" height="230"></div>'; output += '</div>'; output += '<div class="row">'; output += '<div>' + object.product[0].standfirst + '</div>'; output += '</div>'; output += '<div class="row">'; output += '<div class="well"><h2>egenskaper</h2>' + object.product[0].benefits_description + '</div>'; output += '</div>'; (var = 0; < object.product.length; a++) { var product = object.product[a]; var name = product.product_name; output += '<h2>' + name + '</h2>'; (var b = 0; b < product.tabs.length; b++) { var tabs = product.tabs[b]; output += 'tabs<br>'; (var c = 0; c < product.tabs.tab.length; c++) { var tab = tabs.tab[c]; output += 'tab<br>'; (var d = 0; d < product.tabs.tab.contents.length; d++) { var contents = tab.contents[d]; output += 'contents<br>'; (var e = 0; e < product.tabs.tab.contents.content.length; e++) { var content = contents.content[e]; output += 'content<br>'; (var f = 0; f < product.tabs.tab.contents.content.title.length; f++) { var title = content.title[f]; var bodytext = content.bodytext[f]; output += '<h3>' + title + '</h3>'; output += bodytext; } } } } } } output += '</div>'; document.getelementbyid("output").innerhtml = output; } </script>
your second loop not valid. per data in link, product.tabs
not array. object. can't loop , object need have different way process object.
for (var = 0; < object.product.length; a++) { var product = object.product[a]; var name = product.product_name; output += '<h2>' + name + '</h2>'; output += 'tabs<br>'; var tabs = product['tabs']['tab']; (var c = 0; c < tabs.length; c++) { var tab = tabs[c]; output += 'tab<br>'; var contents = tab['contents'] != undefined ? tab['contents']['content'] : []; output += 'contents<br>'; (var e = 0; e < contents.length; e++) { var content = contents[e]; output += 'content<br>'; var title = content.title; var bodytext = content.bodytext; output += '<h3>' + title + '</h3>'; output += bodytext; } } }
Comments
Post a Comment