user2584538 user2584538 - 5 months ago 7
JSON Question

How to create JSON array..?

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.

Answer

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" } ]
Comments