Js 递归遍历Json所有的key 和 value

Js 递归遍历Json所有的key 和 value

分析:

遍历json所有的key,如果当前层的某个key对应的值是object或者数组的话就继续递归,如果其他值就是具体的值了!

实现代码:


// 遍历解析Json
function parseJson(jsonObj) {
    // 循环所有键
    for(var key in jsonObj) {
        //如果对象类型为object类型且数组长度大于0 或者 是对象 ,继续递归解析
        var element = jsonObj[key];
        if(element.length > 0 && typeof(element) == "object" || typeof(element) == "object") {
            parseJson(element);
        } else { //不是对象或数组、直接输出
            console.log("----eles -->  " + key + ":" + element + " ");
        }

    }
}

是不是感觉超简单!!!!

猜你喜欢

转载自blog.csdn.net/qq_35170213/article/details/80868154
今日推荐