45

I have an array of arrays which I have planted in the DOM, so that I can reuse it at a later stage to transfer to the server:

[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]

If I would like to convert it back into a Javascript array, how would I go about doing this?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Donal.Lynch.Msc
  • 3,365
  • 12
  • 48
  • 78
  • 4
    That's not JSON. That already is an array. – Diodeus - James MacFarlane Feb 23 '12 at 20:19
  • 1
    @Diodeus: *"...which I have planted in the DOM..."* I'm guessing OP is showing text content that is hidden somewhere in the page. –  Feb 23 '12 at 20:20
  • 2
    @tereško: I don't think that's a duplicate. Seems that OP is going the other direction. –  Feb 23 '12 at 20:23
  • 1
    @amnotiam you're correct, found a correct duplicate – jondavidjohn Feb 23 '12 at 20:25
  • 5
    @tereško: Here's the deal. I don't care about rep. I wish this site didn't have this meaningless rep system. Fact is that sometimes it's just faster to answer the question than go searching around. Seems like the vast majority of the questions here *(in the JS/jQuery tag anyway)* are posted by people who just don't want to put forth the effort to find the answer themselves. Since StackOverflow doesn't *really* have any way of preventing duplicates *(since most dupes are never closed)*, I figure we might as well just answer the thing, and get it out of the way. –  Feb 23 '12 at 20:33

4 Answers4

74
var obj = $.parseJSON('[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]')

Assuming jquery is ok to use because of tag.

jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
58

If the browzer has the JSON object then

JSON.parse(string);

or if you have jQuery

$.parseJSON(string);
zellio
  • 31,308
  • 1
  • 42
  • 61
  • 1
    If anyone wants to know the difference between the two functions (apart from that one requires jQuery) [this is quite a good read](https://stackoverflow.com/a/10362313/3722330) – A Friend Nov 20 '20 at 09:32
17
var array = JSON.parse(my_JSON)
14

jQuery.parseJSON()

var myArr = $.parseJSON(myJsonString);
simshaun
  • 21,263
  • 1
  • 57
  • 73