最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Create array from JSON - Javascript - Stack Overflow

programmeradmin10浏览0评论

I have a JSON file that I would like to create an array from.

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 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? Thanks

I have a JSON file that I would like to create an array from.

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 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? Thanks

Share Improve this question edited Nov 10, 2011 at 21:55 gberg927 asked Nov 10, 2011 at 21:22 gberg927gberg927 1,6869 gold badges38 silver badges52 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

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.

This assumes that you've stored the data from your question in a variable data. If you only have it as a JSON string, you'll need to convert it with JSON.parse.

JSON is basically javascript already, so once you've decoded the JSON string back to a native JS data structure, then it'd be a simple matter of doing:

var rowsarray = decoded_json.table.rows;
发布评论

评论列表(0)

  1. 暂无评论