2013-08-19 74 views
0
  { 
      "wordsacross": [ 
       {"ACHE": [ 
        { "letter":"A" , "square":"A1" }, 
        { "letter":"C" , "square":"A2" }, 
        { "letter":"H" , "square":"A3" }, 
        { "letter":"E" , "square":"A4" } 
       ]}, 
       {"OPT": [ 
        { "letter":"O" , "square":"A6" }, 
        { "letter":"P" , "square":"A7" }, 
        { "letter":"T" , "square":"A8" } 
       ]} 
      ], 
      "wordsdown": [ 
       {"ALPHA": [ 
        { "letter":"A" , "square":"A1" }, 
        { "letter":"L" , "square":"B1" }, 
        { "letter":"P" , "square":"C1" }, 
        { "letter":"H" , "square":"D1" }, 
        { "letter":"A" , "square":"E1" } 
       ]}, 
       {"BRO": [ 
        { "letter":"B" , "square":"G1" }, 
        { "letter":"R" , "square":"H1" }, 
        { "letter":"O" , "square":"I1" } 
       ]} 
      ] 
      } 

      $.ajax({ 
       type: "POST", 
       url: "query.words.php", 
       data: { puzzleid: vPuzzleId }, 
       async: false 
      }).done(function(msg) { 
       vWords = JSON.parse(msg); 
       console.log(vWords); 
       console.log("There are "+vWords["wordsacross"].length+" words across"); 
       for(var i=0;i<vWords["wordsacross"].length;i++) 
       { 
        console.log(vWords["wordsacross"][i].length); 
        console.log(vWords["wordsacross"][i][0]["square"]); 
       } 
      }); 

我試圖將所有平方項的內容打印到控制檯。我對console.log的兩次嘗試都是未定義的。我如何訪問每個方塊並將其打印到控制檯?在JavaScript中獲取嵌套JSON數組的長度

在此先感謝...

+0

如果添加代碼'會發生什麼console.log(msg)'在行'vWords = JSON.parse(msg)'之前''? – zzlalani

回答

5

vWords['wordsacross']vWords.wordsacross(相當於)包含一個陣列的兩個元素。當你寫vWords['wordsacross'][i]你正在訪問這些項目中的一個。然後,您嘗試訪問該單個項目的length[0],但該項目不是數組,而是一個對象。

對於i = 0它是一個對象,它具有名爲ACHE的屬性是一個數組。

您可能因而寫:

vWords.wordsacross[0].ACHE.length 

您的對象的結構的方式,包含字母陣列的屬性是一個不同的一個用於所述陣列中的每個項目,其可以是有點不方便。你可以用Object.keys(vWords.wordsacross[i])來枚舉對象自己的屬性,但是我建議改變你的對象,如果這是一個選項。

例如,wordsacross數組中的一個項目可能有word屬性,它的值是ACHEletters屬性,它的值將是你的字母排列。這樣一來,你就可以訪問vWords.wordsacross[i].letters而不必知道這個詞恰好是"ACHE"

"wordsacross": [ 
    {"word": "ACHE", 
    "letters": [ 
     { "letter":"A" , "square":"A1" }, 
     { "letter":"C" , "square":"A2" }, 
     { "letter":"H" , "square":"A3" }, 
     { "letter":"E" , "square":"A4" } 
    ]} 
], 

由於字母"A""C""H""E"可以從字推斷"ACHE"你可以得到程與剛剛寫入:

"wordsacross": [ 
    { "word": "ACHE"; 
     "squares": ["A1", "A2", "A3", "A4"] 
    } 
] 

字符串"ACHE"可以被視爲字符的陣列;你可以得到它的length,你可以在任何給定的位置,wordsacross[0].word[i]訪問carachter。

+0

是的,我認爲該對象需要改變。我不知道它應該是什麼樣子。 – Giuseppe

0

嘗試vWords.wordsacross只(無支架):)

1

http://jsfiddle.net/xp8Ww/

for(var i=0;i<vWords["wordsacross"].length;i++) 
      { 
       var keys =Object.keys(vWords["wordsacross"][i]); 
       console.log(keys.length); 
       for(var j=0;j<keys.length;j++){ 
        var keys2=vWords["wordsacross"][i][keys[j]].length; 
        for(var k=0;k<keys2;k++){ 
         console.log(vWords["wordsacross"][i][keys[j]][k]["square"]); 
        } 
       } 

      } 
0

試試這個

for (key in vWords) { //Iterate for wordsacross and wordsdown 
    console.log(key); 
    var words = vWords[key]; //Get the value for wordsacross and wordsdown 
    for (var i = 0; i < words.length; i++) { //Iterate over the words 
     for (word in words[i]) { //Iterate over the word object 
      console.log(word); 
      var wordSquares = words[i][word]; //Get the letter and square info for word object 
      for (var j = 0; j < wordSquares.length; j++) { //Iterate over the letters 
       console.log(wordSquares[j].letter); 
       console.log(wordSquares[j].square); 
      } 
     } 
    } 
} 

這將遍歷所有詞語的結果並打印信和廣場相應