Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 递归问题;解析JSON_Javascript_Json_Recursion - Fatal编程技术网

Javascript 递归问题;解析JSON

Javascript 递归问题;解析JSON,javascript,json,recursion,Javascript,Json,Recursion,我有一个json对象,如下所示: [{ "thing": "Top", "data": { "childs": [{ "thing": "a", "data": { "text": "sdfgdg1", "morestuff": { "thing": "Top", "data

我有一个json对象,如下所示:

[{
    "thing": "Top",
    "data": {
        "childs": [{
            "thing": "a",
            "data": {
                "text": "sdfgdg1",
                "morestuff": {
                    "thing": "Top",
                    "data": {
                        "childs": [{
                            "thing": "a",
                            "data": {
                                "text": "sdfg2",
                                "morestuff": "",
                            }
                        },
                        {
                            "thing": "a",
                            "data": {
                                "text": "gfhjfghj3",
                                "morestuff": {
                                    "thing": "Top",
                                    "data": {
                                        "childs": [{
                                            "thing": "a",
                                            "data": {
                                                "text": "asdfsadf 2 4",
                                                "morestuff": {
                                                    "thing": "Top",
                                                    "data": {
                                                        "childs": [{
                                                            "thing": "a",
                                                            "data": {
                                                                "text": "asdfsadf 2 5",
                                                                "morestuff": {
                                                                    "thing": "Top",
                                                                    "data": {
                                                                        "childs": {
                                                                            "thing": "a",
                                                                            "data": {
                                                                                "text": "asdfsadf 2 6",
                                                                                "morestuff": "",
                                                                            },
                                                                            "data": {
                                                                                "text": "asdfsadf 2 6",
                                                                                "morestuff": "",
                                                                            }
                                                                        },
                                                                    }
                                                                },
                                                            }
                                                        }],
                                                    }
                                                },
                                            }
                                        }],
                                    }
                                },
                            }
                        }],
                    }
                },
            }
        },
        {
            "thing": "a",
            "data": {
                "text": "asdfasd1 2",
                "morestuff": {
                    "thing": "Top",
                    "data": {
                        "childs": [{
                            "thing": "a",
                            "data": {
                                "text": "asdfsadf 2 3",
                                "morestuff": "",
                            }
                        }],
                    }
                },
            }
        },
        {
            "thing": "a",
            "data": {
                "text": "dfghfdgh 4",
                "morestuff": "",
            }
        }],
    }
}]  
…我正试图遍历它,得到“文本”对象的总数

我似乎无法使某些东西正常工作。。我想我缺少对json和递归的基本理解

经过几天的变化后:

count=0;
c2=0;
c3=0;
function ra(arr){
    //console.log(arr.data.morestuff)
    if(arr!==undefined && arr.data && arr.data.morestuff===""){
        c3++;

    }else if((arr && arr.data && typeof arr.data.morestuff==="object")){
            if(arr.data.morestuff.data.childs.length>1){
                for(var w=0;w<arr.data.morestuff.data.childs.length;w++){
                    count+=ra(arr.data.morestuff.data.childs[w])
                }
            }else{
                count+=ra(arr.data.morestuff.data.childs[0])
            }
    }
         return(c3)
}
countn=0;//top morestuff with no morestuff
tot=0;
function reps(obj){
tot=obj.data.childs.length;
console.log("tot="+tot)
    for(var x=0;x<tot;x++){
        tot+=ra(obj.data.childs[x])
        c3=0
        if(tot>1000){//trying to prevent a runaway loop somehwere
            break;
        }
    }
    console.log(tot)
}

reps(json[0]); 
count=0;
c2=0;
c3=0;
功能ra(arr){
//console.log(arr.data.morestuff)
if(arr!==未定义&&arr.data&&arr.data.morestuff===“”){
c3++;
}else if((arr&&arr.data&&typeof arr.data.morestuff==“对象”)){
if(arr.data.morestuff.data.childs.length>1){
对于(var w=0;w

理想情况下,我想计算每个文本对象,它的相对位置,以及它拥有的子对象的数量,但我想如果我能让计数工作正常的话,我可能会把这些东西放到数组中

注意:我尝试过jsonParse和其他库,但没有任何效果。特别是,jsonParse在尝试在此json上使用它时抛出一个
对象没有方法“匹配”
错误。

如果您只想在任何深度使用所有
“text”
属性,那么这就足够了:

但是,您有两个属性键(
“data”
,在嵌套最深的对象中)。由于一个对象不能包含具有相同键的两个属性,因此实际上您有9个
“text”
属性,而不是10个

var count = 0;

function iterate(obj) {
    for(var key in obj) { // iterate, `key` is the property key
        var elem = obj[key]; // `obj[key]` is the value

        if(key === "text") { // found "text" property
            count++;
        }

        if(typeof elem === "object") { // is an object (plain object or array),
                                       // so contains children
            iterate(elem); // call recursively
        }
    }
}

iterate(data); // start iterating the topmost element (`data`)

console.log(count); // 9
下面是一个使用

//const objectScan=require('object-scan');
const data=[{“thing”:“Top”,“data”:{“childs”:[{“thing”:“a”,“data”:{“text”:“sdfgdg1”,“morestuff”:{“thing”:“Top”,“data”:{“childs”:[{“thing”:“a”,“data”:{“text”:“sdfg2”,“morestuff”:“},{“thing”:“a”,“data”:{“text”:“gfhjfgffghjhj3”,“morestuff”:“Top”,“data”:{“thing”:“thing”:“Top”,“data”:{“childs”:“thing”:“a”,“data”:“data”:“{“text”:“text”:“asdfsadfsadfs4”,“morestuff”:“data”:{“孩子们”的“孩子们”的“孩子们”的“::[“东西东西东西东西“:,,,,,”儿童们的“孩子们”的“孩子们”的“东西“,,,,,,,,,,,,,,:::“““,”东西东西东西东西““,,,,,,,,“数据”数据““,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,数据“{”正文:“dfghfdgh 4”,“morestuff”:“}}]}];
const getCount=(obj)=>objectScan(['**.text'],{rtn:'count'})(obj);
console.log(getCount(data));
//=>9
。作为控制台包装{最大高度:100%!重要;顶部:0}

您在最嵌套的对象中有两个
数据
属性-对吗?@pimvdb是的,但是在任何给定的
更多的
属性中都可以有任意数量的
数据
道具非常感谢您!而且,我很难过您这么快就回答了这个问题。@Stormdrage:对不起,我猜:)@primvdb:我顺其自然:)不过,说真的,我有预感这是对JSON概念上的误解,你做了我没有找到的教程、没有试验、没有其他答案的事情:用简单的代码(和记录时间)简洁地解释它。所以,非常严肃地说,谢谢你:)……这绝对是我的“啊哈!“JSON时刻。这不是迭代数组的方式,因为在某些浏览器中原型属性和方法是可枚举的,并且会自己迭代。即使在性能良好的浏览器中,许多js库也会将属性添加到array.prototype中,并且不介意将它们设置为不可枚举