简体   繁体   English

JSON Parse不工作,解析二维数组

[英]JSON Parse not working, parsing two dimensional array

I am trying to parse this LOCAL XMLhttprequest, I am getting back the right response text and displaying it in safari. 我正在尝试解析这个LOCAL XMLhttprequest,我正在找回正确的响应文本并在safari中显示它。 When I make an object to JSON.parse() the responsetext,I am getting errors like "unidentified token '"' " or "expected '}' " no matter how I change the .txt file, it will not parse into an object for me 当我向JSON.parse()响应文本创建一个对象时,我收到的错误如“unidentified token'”'或'expected'}'“无论我如何更改.txt文件,它都不会解析为对象为了我

I have tried to change the .txt to the right JSON format with no luck 我试图将.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"]]};

here is my JOSN .txt 这是我的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;

}

here is my simple function, I first display the response, and then it errors when I am trying to parse the data. 这是我的简单函数,我首先显示响应,然后在我尝试解析数据时出错。 Thanks 谢谢

I expect an object that I can .computerGrid or .playerGrid. 我期待一个对象,我可以.computerGrid或.playerGrid。

You have some errors in your json, just search by "2, , there are 2 occurences, it should be "2", . 你的json中有一些错误,只搜索"2, ,有2个出现,它应该是"2", ,.
You are missing a closing " . 你错过了一个结束"

Also, in order to the JSON to be valid, it should look like this: 此外,为了使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"]]}

Keys, playerGrid and computerGrid must be between " . 键, playerGridcomputerGrid必须介于"

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

  2. Your JSON is incorrect, you are missing quotes around some numbers like "2, 您的JSON不正确,您缺少某些数字的引号,例如"2,

  3. Object Keys "playerGrid" and "computerGrid" must be quoted too 对象键"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: you can always validate your json with codebeautify.org/jsonvalidator PS:您可以随时使用codebeautify.org/jsonvalidator验证您的json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM