javascript - 如何从 JSON 中获取值?

标签 javascript php jquery json

我一直在尝试获取从 PHP 脚本创建并用 JavaScript 处理的 JSON 的值

{
    "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": []
            }]
        }]
    }]
}

当我调用包含 JavaScript 的 HTML 时,只会返回:

undefined, undefined, undefined, undefined, undefined, undefined, undefined,<br/> undefined, undefined, undefined, undefined, undefined, undefined, undefined,<br/> undefined, undefined, undefined, undefined, undefined, undefined, undefined,

我应该如何迭代子标签?

我的 JavaScript 如下:

   <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>');
            }
        });
    });
});

希望你们能帮助我:D

最佳答案

尝试在each()中使用data.children。

    $(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>');

            });
        });
    });

如果您使用 $.each(data, ... 您正在迭代该对象,并且需要迭代子列表

这是使用递归函数 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 - 如何从 JSON 中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26785480/

相关文章:

javascript - JavaScript 中的时尚弹出窗口?

php - parse_ini_file() 出于安全原因已被禁用 - 替代方案?

javascript - 使用 codeigniter 进行 JQuery ajaxfileupload

NGINX:1GB 后下载停止 - 上游超时(110:连接超时)

JavaScript:设置 cookie 不起作用

javascript - 选中复选框时删除部分表单

java - 如何在单击按钮时导出到 .excel 时添加 openwith 并另存为对话框

javascript - Nodejs require 未全局加载对象

javascript - 为前端组件实现树形数据结构

javascript - 将存储在 XML 中的数据传递给 onCellSelect