{
"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數組的長度
在此先感謝...
如果添加代碼'會發生什麼console.log(msg)'在行'vWords = JSON.parse(msg)'之前''? – zzlalani