admin管理员组

文章数量:1405393

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.

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.

Share Improve this question asked Jun 9, 2016 at 12:11 WP LearnerWP Learner 8301 gold badge14 silver badges39 bronze badges 3
  • 1 Possible duplicate of 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 Commented Jun 9, 2016 at 12:14
  • Does this help? stackoverflow./questions/2250953/… – Ian Commented Jun 9, 2016 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 Commented Jun 9, 2016 at 12:22
Add a ment  | 

1 Answer 1

Reset to default 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" } ]

本文标签: javascriptHow to create JSON arrayStack Overflow