JSON解析不起作用,解析二维数组

时间:2019-04-06 22:14:03

标签: javascript html json

我正在尝试解析此LOCAL XMLhttprequest,我正在获取正确的响应文本并将其显示在safari中。 当我使用JSON.parse()作为响应文本的对象时,无论如何更改.txt文件,都会收到诸如“ unidentified token'””或“ expected'}'“之类的错误,它将不会解析为对象对我来说

我尝试将.txt更改为正确的JSON格式

{playerGrid: [["3","2","2","2","2","2","2","3","3","3"], ["3","2","2","2","2","2","2","2","2","2"], ["3","2","2","2","2","2","2","2","2","3"],["3","2","2","2","2","2","2","2","2","3"], ["4","2","2","2","2","3","2","2","2","3"], ["2","2","2","2","7","3","2","2","2","3"], ["2","2","2","2","7","2","2","2","2","2"], ["2","2","2","3","3","3","2","2","2","2"], ["2","2","2,"2","2","2","2","2","2","2"], ["2","2","2","2","2","2","2","2","2","2"]],
computerGrid: [["2","2","2","7","4","9","9","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","9","9","2","2,"2","2"], ["2","2","2","2","2","2","2","2","2","2"], ["9","9","9","9","9","2","2","2","2","2"], ["2","2","2","2","7","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"]]};

这是我的JOSN .txt

function fileRequest()
{

  var localRequest = new XMLHttpRequest();

  localRequest.open("GET", "sampleJSON.txt", false);

  localRequest.send(null);

  document.getElementById("jsonDiv").innerHTML = localRequest.responseText;

  var jsonObject = JSON.parse(localRequest.response);

  document.getElementById("jsonParsed").innerHTML = jsonObject.computerGrid;

}

这是我的简单函数,首先显示响应,然后在尝试解析数据时出错。 谢谢

我希望我可以使用.computerGrid或.playerGrid的对象。

2 个答案:

答案 0 :(得分:0)

您的json中有一些错误,只需按"2,进行搜索,就会发生2次,应该为"2",
您缺少结束语"

此外,为了使JSON有效,它应如下所示:

{"playerGrid":[["3","2","2","2","2","2","2","3","3","3"],["3","2","2","2","2","2","2","2","2","2"],["3","2","2","2","2","2","2","2","2","3"],["3","2","2","2","2","2","2","2","2","3"],["4","2","2","2","2","3","2","2","2","3"],["2","2","2","2","7","3","2","2","2","3"],["2","2","2","2","7","2","2","2","2","2"],["2","2","2","3","3","3","2","2","2","2"],["2","2","2","2","2","2","2","2","2","2"],["2","2","2","2","2","2","2","2","2","2"]],"computerGrid":[["2","2","2","7","4","9","9","2","2","2"],["2","9","2","2","2","2","2","2","2","2"],["2","9","2","2","2","2","2","2","2","2"],["2","9","2","2","9","9","2","2","2","2"],["2","2","2","2","2","2","2","2","2","2"],["9","9","9","9","9","2","2","2","2","2"],["2","2","2","2","7","2","2","2","9","2"],["2","2","2","2","2","2","2","2","9","2"],["2","2","2","2","2","2","2","2","9","2"],["2","2","2","2","2","2","2","2","9","2"]]}

playerGridcomputerGrid必须在"之间。

答案 1 :(得分:0)

  1. 使用JSON.parse(localRequest.responseText)

  2. 您的JSON不正确,您缺少诸如"2,

  3. 之类的数字的引号
  4. 对象键"playerGrid""computerGrid"也必须加引号

let d = `{
"playerGrid": [
  ["3","2","2","2","2","2","2","3","3","3"], ["3","2","2","2","2","2","2","2","2","2"],
  ["3","2","2","2","2","2","2","2","2","3"], ["3","2","2","2","2","2","2","2","2","3"],
  ["4","2","2","2","2","3","2","2","2","3"], ["2","2","2","2","7","3","2","2","2","3"],
  ["2","2","2","2","7","2","2","2","2","2"], ["2","2","2","3","3","3","2","2","2","2"],
  ["2","2","2","2","2","2","2","2","2","2"], ["2","2","2","2","2","2","2","2","2","2"]],
"computerGrid": [
  ["2","2","2","7","4","9","9","2","2","2"], ["2","9","2","2","2","2","2","2","2","2"],
  ["2","9","2","2","2","2","2","2","2","2"], ["2","9","2","2","9","9","2","2","2","2"],
  ["2","2","2","2","2","2","2","2","2","2"], ["9","9","9","9","9","2","2","2","2","2"],
  ["2","2","2","2","7","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"],
  ["2","2","2","2","2","2","2","2","9","2"], ["2","2","2","2","2","2","2","2","9","2"]]
}`;

console.log(JSON.parse(d))

PS:您始终可以使用codebeautify.org/jsonvalidator

来验证json。