我有这个JSON:
{
"solution": {
"section1": {
"cell-1": "1",
"cell-2": "2",
"cell-3": "3",
"cell-4": "4",
"cell-5": "5",
"cell-6": "6",
"cell-7": "7",
"cell-8": "8",
"cell-9": "9"
},
"section2": {
"cell-1": "1",
"cell-2": "2",
"cell-3": "3",
"cell-4": "4",
"cell-5": "5",
"cell-6": "6",
"cell-7": "7",
"cell-8": "8",
"cell-9": "9"
}
}
}我的代码:
$.getJSON('/static/front/js/src/sudoku/22122016.json', function(data) {
$.each(data.solution.section1[0], function() {
$.each(this, function(cell, cellNumber) {
console.log(cell + ' ' + cellNumber);
});
});
});这段代码给出了以下错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in 1
at isArrayLike (jquery.js:535)
at Function.each (jquery.js:362)
at String.<anonymous> (main.js:61)
at Function.each (jquery.js:371)
at Object.success (main.js:60)
at fire (jquery.js:3187)
at Object.fireWith [as resolveWith] (jquery.js:3317)
at done (jquery.js:8757)
at XMLHttpRequest.<anonymous> (jquery.js:9123)
in jquery.js:535我需要单独访问每个节,所以我需要进入每个节,然后迭代每个键并获得它的值。我怎样才能做到这一点呢?
发布于 2016-12-22 20:42:04
在没有测试的情况下,我会猜测:
$.getJSON('/static/front/js/src/sudoku/22122016.json', function(data) {
$.each(data.solution, function(sectionName, cells) {
$.each(cells, function(cellName, cellNumber) {
console.log(cellName + ' ' + cellNumber);
});
});
});此时,您可能希望考虑通过将其拆分成方法或直接导航到单元格来降低缩进级别。
发布于 2016-12-22 20:35:12
您可以遍历单元格:
$.each(data.solution.section1, function(cell, cellNumber) {
console.log(cell + ' ' + cellNumber)
})发布于 2016-12-22 20:47:55
我不知道您到底想要得到什么,但是将"“放在data.solution.section1的末尾可以防止跨data.solution.section1内容进行索引。
https://stackoverflow.com/questions/41283239
复制相似问题