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
1 Answer
Reset to default 7Surround 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
版权声明:本文标题:javascript - How to create JSON array..? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744271364a2598198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论