将 JSON 解析为多维数组 JS

发布于 2024-12-15 03:58:47 字数 519 浏览 0 评论 0原文

我有一个 JSON 文件,我想从中创建一个多维数组。此信息存储在 JSON 文件中。

这是 JSON 数据。

{
  "table": {
    "columnNames": ["column1", "column2", "column3", "column4"],
    "columnTypes": ["String", "String", "String", "String"],
    "rows": [
      ["data00", "data01", "data02", "data03"],
      ["data10", "data11", "data12", "data13"],
      ["data20", "data21", "data22", "data23"],
      ["data30", "data31", "data32", "data33"]
     ]
   }
}

我需要从“行”部分中的对象创建一个数组数组。

任何帮助将不胜感激!

谢谢!!!

I have a JSON file that I would like to create an multi-dimensional array from. This information is stored in a JSON file.

Here is the JSON data.

{
  "table": {
    "columnNames": ["column1", "column2", "column3", "column4"],
    "columnTypes": ["String", "String", "String", "String"],
    "rows": [
      ["data00", "data01", "data02", "data03"],
      ["data10", "data11", "data12", "data13"],
      ["data20", "data21", "data22", "data23"],
      ["data30", "data31", "data32", "data33"]
     ]
   }
}

I need to create an array of arrays from the objects in the "rows" section.

Any help would be appreciated!

Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔戏命师 2024-12-22 03:58:47

解析 JSON 后,“table.rows”属性将已经是一个多维数组(具体来说是 2 维)。您所要做的就是访问它:

var array2D = parsed.table.rows;

至于解析,您可能应该使用类似 Crockford 的解析器

var parsed = JSON.parse(rawStringOfJSON);

Once you parse the JSON, the "table.rows" property will already be a multi-dimensional array (2 dimensions, to be specific). All you have to do is access it:

var array2D = parsed.table.rows;

As to parsing, you should probably use something like Crockford's parser:

var parsed = JSON.parse(rawStringOfJSON);
幼儿园老大 2024-12-22 03:58:47

首先需要将字符串转换为 JS 对象。

使用 http://www.json.org/js.html 中的 json2.js

请记住,如果您考虑支持旧浏览器,则需要添加 json2.js。较新的浏览器内置了对 JSON 的支持,

并将字符串反序列化为对象

var myObject = JSON.parse(myJSONtext);

,现在您可以访问

myObject.table.rows[1][2]; // yields data12

You first need to convert the string to JS object.

Use json2.js from http://www.json.org/js.html

Please keep in mind that you need to add json2.js if you consider to support old browsers. Newer browser has built-in support for JSON

And deserialize the string to object

var myObject = JSON.parse(myJSONtext);

And now you can access

myObject.table.rows[1][2]; // yields data12
小糖芽 2024-12-22 03:58:47

rows 部分已经包含一个数组数组,所以只需使用:

var result = JSON.parse('{
  "table": {
  "columnNames": ["column1", "column2", "column3", "column4"],
  "columnTypes": ["String", "String", "String", "String"],
  "rows": [
    ["data00", "data01", "data02", "data03"],
    ["data10", "data11", "data12", "data13"],
    ["data20", "data21", "data22", "data23"],
    ["data30", "data31", "data32", "data33"]
   ]
  }
}');

var rows = result.table.rows;

The rows section already contains an array of arrays, so just use:

var result = JSON.parse('{
  "table": {
  "columnNames": ["column1", "column2", "column3", "column4"],
  "columnTypes": ["String", "String", "String", "String"],
  "rows": [
    ["data00", "data01", "data02", "data03"],
    ["data10", "data11", "data12", "data13"],
    ["data20", "data21", "data22", "data23"],
    ["data30", "data31", "data32", "data33"]
   ]
  }
}');

var rows = result.table.rows;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文