2

I've been trying to get the values of this JSON created from a PHP script and treated with 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 I call the HTML that contains the JavaScript just returns:

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

How should I iterate through the children label?

My JavaScript is as 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 you can help me guys :D

drew
  • 31
  • 8

2 Answers2

3

Try with data.children inside the 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 you use $.each(data, ... you are iterating over the object and you need to iterate over the children list

This is the 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);
            });
        });
Ragnar
  • 4,393
  • 1
  • 27
  • 40
  • Now i'm getting 2, 1, categoria raiz, 1, 1, 1, [object Object],[object Object],[object Object],[object Object],[object Object],[object Object], but not whats in the children below and the parent directory... – drew Nov 06 '14 at 17:33
  • Yes, you can do it in the same way! – Ragnar Nov 06 '14 at 17:35
  • it's ok because val.children is an array of objects – Ragnar Nov 06 '14 at 17:52
  • Yes I do, but i don't know how to continue iterating through the next childs... i'm really noob on javascript I tryed to do $.each(data.children.children, function(key, val) but doesn't work – drew Nov 06 '14 at 18:00
  • You can add another each inside the main each or you can create a recursive function and call it when the object has childrens – Ragnar Nov 06 '14 at 18:53
  • wow i'd like to know how to do a recursive function can u explain me how? Please – drew Nov 06 '14 at 19:00
  • Thanks a lot Ragnar for your help. I'll try it and work on it :DDDD – drew Nov 06 '14 at 19:08
1

Finally I get it, thanks a lot Ragnar for ur great help. My script now looks like

<script type='text/javascript'>
$(document).ready(function(){
    $.getJSON('ArbolCategorias.php', function(data) {
        //mejorar para que sea dinamico
        $.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 +  '</li>');
        });


        $.each(data.children, function(key, val) {
            $.each(val.children, function(key, value) {

                    $('#jsonresult').append('<li id="' + key + '">' + value.category_id  + ', ' + value.parent_id + ', ' + value.name + ', ' + value.is_active + ', ' + value.position + ', ' + value.level + '</li>');
            });
        });


        $.each(data.children, function(key, val) {
            $.each(val.children, function(key, value) {
                $.each(value.children, function(key, value) {
                    $('#jsonresult').append('<li id="' + key + '">' + value.category_id  + ', ' + value.parent_id + ', ' + value.name + ', ' + value.is_active + ', ' + value.position + ', ' + value.level + ', ' + value.children + ', ' + '</li>');
                });
            });
        });
    });
});

It's not dinamic so when I introduce a new child it will not get printed but i'll still working on it :D

drew
  • 31
  • 8