[英]Create array from JSON - Javascript
I have a JSON file that I would like to create an array from. 我有一个JSON文件,我想从中创建一个数组。
Here is the JSON data. 这是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 need to create an array from the objects in the "rows" section. 我需要从“行”部分的对象创建一个数组。
Any help would be appreciated! 任何帮助,将不胜感激!
Thanks!!! 谢谢!!!
EDIT 编辑
Would it be possible to create a hash table out of the data in rows? 是否可以从行中的数据中创建哈希表? Also, how would you perform JSON.parse on a json file?
另外,你如何在json文件上执行JSON.parse? Thanks
谢谢
Do you mean you want to get a single array holding all the values? 你的意思是你想得到一个包含所有值的数组吗?
var rows = [];
for (var i = 0; i < data.table.rows.length; i++) {
rows.push.apply(rows, data.table.rows[i]);
}
See MDN docs for push
and apply
. 请参阅MDN文档以进行
push
和apply
。
This assumes that you've stored the data from your question in a variable data
. 这假定您已将问题中的
data
存储在可变data
。 If you only have it as a JSON string, you'll need to convert it with JSON.parse
. 如果您只将它作为JSON字符串,则需要使用
JSON.parse
进行转换。
JSON基本上已经是javascript,所以一旦你将JSON字符串解码回本机JS数据结构,那么它就是一个简单的事情:
var rowsarray = decoded_json.table.rows;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.