javascript - How to get the values from that JSON? -



javascript - How to get the values from that JSON? -

i've been trying values of json created php script , treated javascript

{ "category_id": "1", "parent_id": "0", "name": "root catalog", "is_active": null, "position": "0", "level": "0", "children": [{ "category_id": "2", "parent_id": "1", "name": "categoria raiz", "is_active": "1", "position": "1", "level": "1", "children": [{ "category_id": "14", "parent_id": "2", "name": "nombre1", "is_active": "1", "position": "1", "level": "2", "children": [] }, { "category_id": "12", "parent_id": "2", "name": "nombre2", "is_active": "1", "position": "2", "level": "2", "children": [] }, { "category_id": "11", "parent_id": "2", "name": "nombre3", "is_active": "1", "position": "3", "level": "2", "children": [] }, { "category_id": "10", "parent_id": "2", "name": "nombre4", "is_active": "1", "position": "4", "level": "2", "children": [] }, { "category_id": "7", "parent_id": "2", "name": "nombre5", "is_active": "1", "position": "7", "level": "2", "children": [] }, { "category_id": "6", "parent_id": "2", "name": "nombre6", "is_active": "1", "position": "8", "level": "2", "children": [{ "category_id": "3", "parent_id": "6", "name": "nombre6_1", "is_active": "1", "position": "1", "level": "3", "children": [] }, { "category_id": "5", "parent_id": "6", "name": "nombre6_2", "is_active": "1", "position": "2", "level": "3", "children": [] }, { "category_id": "4", "parent_id": "6", "name": "nombre6_3", "is_active": "1", "position": "3", "level": "3", "children": [] }, { "category_id": "15", "parent_id": "6", "name": "6_4", "is_active": "1", "position": "4", "level": "3", "children": [] }, { "category_id": "16", "parent_id": "6", "name": "nombre6_5", "is_active": "1", "position": "5", "level": "3", "children": [] }] }] }] }

when phone call html contains javascript returns:

undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,

how should iterate through children label?

my javascript follows:

<script type='text/javascript'> $(document).ready(function(){ console.log('entra...'); $.getjson('arbolcategorias.php', function(data) { //console.log('data ...' + data); $.each(data.children, function(key, val) { if (data.children.has(children)){ $.each(children.children, function(key, val){ $('#jsonresult').append('<li id="' + key + '">' + val.category_id + ', ' + val.parent_id + ', ' + val.name + ', ' + val.is_active + ', ' + val.position + ', ' + val.level + ', ' + val.children + ', ' + '</li>'); }); } else{ $('#jsonresult').append('<li id="' + key + '">' + val.category_id + ', ' + val.parent_id + ', ' + val.name + ', ' + val.is_active + ', ' + val.position + ', ' + val.level + ', ' + val.children + ', ' + '</li>'); } }); }); });

hope can help me guys :d

try data.children within each().

$(document).ready(function(){ console.log('entra...'); $.getjson('arbolcategorias.php', function(data) { console.log('data ...' + data); $.each(data.children, function(key, val) { $('#jsonresult').append('<li id="' + key + '">' + val.category_id + ', ' + val.parent_id + ', ' + val.name + ', ' + val.is_active + ', ' + val.position + ', ' + val.level + ', ' + val.children + ', ' + '</li>'); }); }); });

if utilize $.each(data, ... iterating on object , need iterate on children list

this code using recursive function writechildrens:

function writechildrens(element){ $.each(element.children, function(key, val) { $('#jsonresult').append('<li id="' + key + '">' + val.category_id + ', ' + val.parent_id + ', ' + val.name + ', ' + val.is_active + ', ' + val.position + ', ' + val.level + '</li>'); if(val.children.length>0) writechildrens(val); }); } $(document).ready(function(){ console.log('entra...'); $.getjson('arbolcategorias.php', function(data) { console.log('data ...' + data); writechildrens(data); }); });

javascript php jquery json

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -