javascript - 使用 jquery 从没有标识符的 JSON 数组中选择第一个元素

标签 javascript jquery json

我无法访问 DATA 数组中的数字,这将是 COLUMNS 中提到的 ID。

我的JSON如下:

{
  "COLUMNS": [
    "ID",
    "DESCRIPTION",
    "DATE"
  ],
  "DATA": [
    [
      53,
      "Try to do something test123",
      "September, 05 2017 00:00:00 +0100"
    ]
  ]
}

我目前的尝试是这个,但是有了这个,我得到了全部 3 个元素

  var jsonLength= JSON.DATA.length;

  for (dataIndex = 0; dataIndex < jsonLength; dataIndex++) {

      var dataLength= JSON.DATA[dataIndex].length;

      for (noteIndex = 0; noteIndex < dataLength; noteIndex++) {

          alert(JSON.DATA[dataIndex]);

      }
  }

最佳答案

您的代码几乎是正确的,您只是缺少二维 DATA 数组上的第二个索引访问器。您可以在循环中使用 noteIndex 递增变量:

var JSON = {
  "COLUMNS": [ "ID", "DESCRIPTION", "DATE" ],
  "DATA": [ 
    [ 53, "Try to do something test123", "September, 05 2017 00:00:00 +0100" ] 
  ]
}
var jsonLength = JSON.DATA.length;

for (dataIndex = 0; dataIndex < jsonLength; dataIndex++) {
  var dataLength = JSON.DATA[dataIndex].length;
  for (noteIndex = 0; noteIndex < dataLength; noteIndex++) {
    console.log(JSON.DATA[dataIndex][noteIndex]); // note the additional [] here
  }
}

关于javascript - 使用 jquery 从没有标识符的 JSON 数组中选择第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46050605/

相关文章:

javascript - David Shariff 的 JavaScript 测验问题

javascript - 为什么我得到 "map.set_center is not a function"?

javascript - mongoDB 和 Express 的请求尚未完成错误

jquery - 在提交警报时选择标签

Javascript 'this' 上下文与 jQuery

javascript - 从 json 文件绑定(bind)单个 Angular 表达式

javascript - 如何将 log4javascript 与 ScalaJS/PhantomJS 一起使用

javascript - 设置cookie并使用jquery将其加载到另一个页面中

javascript - 我使用哪个Youtube API搜索视频?

json - 有时对于两种不同的数据类型使用相同的 SerializedName,但使用 Kotlin