将 JSON 解析为多维数组 JS
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解析 JSON 后,“table.rows”属性将已经是一个多维数组(具体来说是 2 维)。您所要做的就是访问它:
至于解析,您可能应该使用类似 Crockford 的解析器 :
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:
As to parsing, you should probably use something like Crockford's parser:
首先需要将字符串转换为 JS 对象。
使用 http://www.json.org/js.html 中的
json2.js
请记住,如果您考虑支持旧浏览器,则需要添加 json2.js。较新的浏览器内置了对 JSON 的支持,
并将字符串反序列化为对象
,现在您可以访问
You first need to convert the string to JS object.
Use
json2.js
from http://www.json.org/js.htmlPlease 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
And now you can access
rows 部分已经包含一个数组数组,所以只需使用:
The rows section already contains an array of arrays, so just use: