Note that there are some explanatory texts on larger screens.

plurals
  1. POIterating over JSON result
    text
    copied!<p>I parse XML ( <a href="http://gyazo.com/fe38f0eb89a1f1f88927f676f304cc1c" rel="nofollow">http://gyazo.com/fe38f0eb89a1f1f88927f676f304cc1c</a> ) to JSON with a plugin. I want it to loop through all categories and for each loop cycle repeat the process for child categories and props, it should be doing this infinitely so with a loop.</p> <p>I have looked into recursive functions and it's what I am attempting but it's not working for me cause the console.log always says that it's undefined as soon as I try $.each to get the child elements.</p> <p>JQuery</p> <pre><code>function ParseData(v) { var caption = v.caption; console.log('Category ' + caption + ' parsed.'); if (v.prop != undefined) { console.log('Prop(s) found, parsing....'); $.each(v.prop, function(key, value) { ParseData(v.prop); }); } else if (v.cat != undefined) { console.log('Child(s) found, parsing....'); $.each(v.cat, function(key, value) { ParseData(v.cat); }); } } function loadProps() { //variables var pb = $('div#prop_browser'); //parse prop categories console.log('Parsing Categories....'); $.get("prop-cat.xml", function(xml) { var data = $.xml2json(xml); $.each(data, function(key, value) { $.each(value, function(key, value) { ParseData(value); }); }); }); } </code></pre> <p>I have literally spent hours and hours of googling and coding but I cannot figure this out and I doubt that it is my logic....</p> <p>Please help me!</p> <p>Sample JSON (used JSON.stringify instead of ParseData): First JSON string has two categories, one prop. Second JSON string has one category and one prop.</p> <pre><code>{"cat":[{"id":"0","caption":"Dog 1","img":""},{"id":"0","caption":"Dog 9","img":""}],"prop":{"caption":"Prop 1","img":""},"id":"0","caption":"Cat 1","img":""} {"cat":{"id":"0","caption":"Dog 1","img":""},"prop":{"caption":"Prop 2","img":""},"id":"0","caption":"Cat 2","img":""} </code></pre> <p>@charlietfl's JSFiddle, doesnt work wth is going on!</p> <pre><code>function processData() { $(pb).append('&lt;div class="prop_category"&gt;' + $(this).attr('caption')); if ($(this).children().length &gt; 0) { $(this).children().each(processData); } else { $(pb).append('&lt;/div&gt;'); } } function loadProps() { //variables pb = $('#prop_browser'); $.get("prop-cat.xml", function(xml) { $(xml).children().each(processData); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload