2011-12-11 85 views
4

我有一个JSON对象是这样的:递归问题;解析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]); 

我得出结论,我只是不知道。我得到各种不同的结果;有些人通过将ra方法的回报加在一起而接近,但没有任何一致(即错误),并且总是至少少数。

JSON是一致的,虽然有未知数的儿童和儿童的孩子,这就是为什么我期待递归。

这里是一个小提琴:http://jsfiddle.net/CULVx/

理想情况下,我想统计每个文本对象,它的相对位置,而且它有子女的数目,但我想我可以用得到的东西进入混乱一个数组,如果我只能得到计数工作...

注:我试过jsonParse和其他库无济于事。特别是,当试图在这个json上使用它时,jsonParse会抛出一个Object has no method "match"错误。

+2

窝窝......只是用GSON人! – FUD

+0

在最嵌套的对象中有两个'data'属性 - 是否正确? – pimvdb

+0

我没有使用java – stormdrain

回答

5

如果你只想在任何深度所有"text"属性,那么这应该是足够了:http://jsfiddle.net/QbpqT/

虽然(最大嵌套对象中有"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 
+0

非常感谢!另外,我非常难过,你很快就回答了这个问题。 – stormdrain

+0

@stormdrain:对不起,我猜:) – pimvdb

+0

@primvdb:我会让它滑动:)虽然,我有一个预感,这是一个概念性的JSON理解错误的理解,你做了什么没有教程,我发现,没有试验,我找不到其他答案:简单地用简单的代码解释它(并且在创纪录的时间!)。所以再次,非常认真,谢谢:) ......这绝对是我的“啊!” JSON的时刻。 – stormdrain