解析JSON多级数组

时间:2015-11-25 14:16:51

标签: json parsing

我想在多个级别的数组中搜索json数据。我的搜索列表返回我的对象​​的名称,但仅从第一级返回。我怎么能回复所有对象的名字,无论他们的级别如何? 在此示例中:OST,OST详细信息,Apocalpse Now,Arizona Dream,Dexter

数据

<script type="application/json" id="dataMusic">
{
"name":"Music",
"level":"1",
"size":36184,
"children":[
    {
        "name":"OST",
        "level":"2",
        "size":1416,
        "children":[
            {
                "name":"OST details",
                "level":"3",
                "size":1416,
                "children":[
                    {
                        "name":"Apocalypse Now",
                        "size":15
                    },
                    {
                        "name":"Arizona Dream",
                        "size":19
                    },
                    {
                        "name":"Dexter",
                        "size":20
                    }
                ]
             }
          ]
       }
    ]
 }
 </script>

功能

var dataMusic = document.getElementById('dataMusic').innerHTML; 
var dataTree = JSON.parse(dataMusic);   

var optArray = [];
for (var i = 0; i < dataTree.children.length - 1; i++) {
    optArray.push(dataTree.children[i].name);
    }
    optArray = optArray.sort();

我尝试使用此方法Parsing Nested Objects in a Json using JS但没有成功

功能

var optArray = [], Music, OST, OST details;
for (Music in dataTree) {
    for (OST in dataTree[Music]) {
        for (OST details in dataTree[Music][OST]) {
            if (OST details in optArray) {
                optArray[OST details].push(dataTree[Music][OST][OST details].name)
            } else {
                    optArray[OST details] = [dataTree[Music][OST][OST details].name] 
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须使用嵌套循环

for Music.children.length
    for OST.children.length
        for OST details.children.length

编辑功能

var optArray = [], Music, OST, OST_details;
for (Music in dataTree) {
    for (OST in dataTree[Music]) {
        for (OST_details in dataTree[Music][OST]) {
            if (OST_details in optArray) {
                optArray[OST_details].push(dataTree[Music][OST][OST_details].name)
            } else {
                    optArray[OST_details] = [dataTree[Music][OST][OST_details].name] 
            }
        }
    }
}

答案 1 :(得分:0)

我明白了

var dataMusic = document.getElementById('dataMusic').innerHTML; 
var dataTree = JSON.parse(dataMusic);  
var result = [];   

function getAll( input, target ) {     
    function parseData( input, target ) {
        $.each( input, function ( index, obj ) {
            if ( index == target ) {
                result.push( obj );
            }
            else {
                switch ( $.type( obj ).toLowerCase() ) {
                    case "object":
                    case "array":
                        parseData( obj, target );
                        break;
                }
            }
        });
    }    
    parseData( dataTree, "name" );
    result = result.sort();
    return result;
}
alert(JSON.stringify( getAll( dataTree, "name" )));

感谢这篇文章: Parsing multi-level json; Demo