我使用 ajax 请求从服务器端获取 JSON 数据,并在响应中将其发送到客户端。
我正在尝试循环遍历包含多个 JSON 对象的数组,我现在需要将这些对象存储在变量中,以便将其显示在 html 表中。
作为测试,我确保可以使用以下方法正确访问 JSON 数据:
console.log("JSON: "+ data[0].issue);//这会打印正确的数据
作为辅助措施,我在开始 for 循环之前测试了数组的长度:
console.log("数据数组长度:"+ data.length);//这会打印正确的数组长度
我的 JSON 数据结构如下:
[
{"issueid":"5","username":"Kevin McCluney","issue":"Tables opening & closing when clicked","dateopened":"2017-08-04T05:00:00.000Z","type":"Improper Display","priority":"Medium","issuestatus":"Not Started","comments":"When a table is first expanded it appears, disappears, then reappears – that’s distracting; have it appear only once when expanding."},
{"issueid":"4","username":"Kevin McCluney","issue":"Allow users to re-arrange columns \"on the fly\"","dateopened":"2017-08-04T05:00:00.000Z","type":"Feature Request","priority":"Low","issuestatus":"Not Started","comments":"It would be nice if the column order could be changed on-the-fly (e.g., by dragging). I consider this low priority. Developer would have to decide if the re-arrangement affects other tables at the same time."},
{"issueid":"3","username":"Kevin McCluney","issue":"Table data display and alignment","dateopened":"2017-08-04T05:00:00.000Z","type":"Improper Display","priority":"Medium","issuestatus":"Not Started","comments":"The table data columns need to be aligned in the tables."},
{"issueid":"2","username":"Kevin McCluney","issue":"Additional features open in separate tabs","dateopened":"2017-08-04T05:00:00.000Z","type":"Improper Display","priority":"Low","issuestatus":"Not Started","comments":"The documentation and bug tracking features should be opened in new tabs to let the user continue viewing the table data where the user left off."},
{"issueid":"1","username":"Kevin McCluney","issue":"Requesting user login to view Documentation & Bug Database","dateopened":"2017-08-04T05:00:00.000Z","type":"Logic Error","priority":"Medium","issuestatus":"Not Started","comments":"The user shouldn't have to login again when opening the documentation or bug tracking features."}
]
在我看来,在通过将数据数组打印到控制台来成功测试数据数组之后,我需要做的就是循环遍历数据数组并将存储在数据中的每个项目存储在变量中并根据需要显示它到我的 table 上。
但是,当我这样创建 for 循环时:
// Here i am getting 'Uncaught SyntaxError: Unexpected Identifier'
// What gives?????????/
for (int i = 0; i < data.length; i++){
console.log("JSON: " + data[i].issueid);
}
我收到如下语法错误:
'Uncaught SyntaxError: Unexpected Identifier'
如何正确从多个 JSON 对象中提取数据???
最佳答案
int
不是 JavaScript 中的东西。请改用 var
或 let
。
for (let i = 0; i < data.length; i++){
console.log("JSON: " + data[i].issueid);
}
关于javascript - 为什么在循环访问数组中的 JSON 对象时会出现语法错误?如何正确提取 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45530073/