2011-12-11 135 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