在JavaScript中获取嵌套JSON数组的长度

时间:2013-08-19 04:22:20

标签: javascript arrays json

            {
            "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的两次尝试都是未定义的。我如何访问每个方块并将其打印到控制台?

提前致谢...

4 个答案:

答案 0 :(得分: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属性,其值为ACHE,而letters属性的值为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],你可以在任何给定的位置{{1}}访问carachter。

答案 1 :(得分: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"]);
                    }
                }

            }

答案 2 :(得分:0)

仅尝试vWords.wordsacross(无括号):)

答案 3 :(得分: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);
            }
        }
    }
}

这将迭代结果中的所有单词并相应地打印字母和正方形