1

I am creating a localStorage variable and always push data to it where I am collecting.

Here is my data set and it is dynamically generating.

{ 'my_id' : '79', 'cc_id' : '57223' }, { 'my_id' : '79', 'cc_id' : '57249' }, { 'my_id' : '79', 'cc_id' : '57250' }, { 'my_id' : '79', 'cc_id' : '57221' }, { 'my_id' : '79', 'cc_id' : '57220' }

I need to convert this to a json array.

How could I do so. I tried but couldn't find out a way.

WP Learner
  • 788
  • 1
  • 13
  • 34
  • 1
    Possible duplicate of [Convert JS object to JSON string](http://stackoverflow.com/questions/4162749/convert-js-object-to-json-string) If you check out the answer to that question, you'll see there is a built-in method in JavaScript to convert objects and arrays to JSON. – forgivenson Jun 09 '16 at 12:14
  • Does this help? http://stackoverflow.com/questions/2250953/how-do-i-create-javascript-arrayjson-format-dynamically – Ian Jun 09 '16 at 12:15
  • @forgivenson This is not a duplicate question and I searched in the link you provided too and tried the ways on it before ask this question. – WP Learner Jun 09 '16 at 12:22

1 Answers1

7

Surround your data with [ and ].

Also single quotes are not allowed, only double quotes.

So here, what you'll have:

[ { "my_id" : "79", "cc_id" : "57223" }, { "my_id" : "79", "cc_id" : "57249" }, { "my_id" : "79", "cc_id" : "57250" }, { "my_id" : "79", "cc_id" : "57221" }, { "my_id" : "79", "cc_id" : "57220" } ]
ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35