How to convert this string into original array in Javascript?
var str_arr = "["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410]"
like I want to store it back as original array
var arr = ["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410];
How to convert this string into original array in Javascript?
var str_arr = "["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410]"
like I want to store it back as original array
var arr = ["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410];
Share
Improve this question
asked Dec 16, 2016 at 19:25
Umair JameelUmair Jameel
1,6733 gold badges31 silver badges58 bronze badges
4
- 1 What do you mean by "original" array in your first sentence? – Neo Commented Dec 16, 2016 at 19:27
- I mean, remove double quotes. – Umair Jameel Commented Dec 16, 2016 at 19:28
- Possible duplicate of Safely turning a JSON string into an object – Андрей Беньковский Commented Dec 16, 2016 at 19:37
- 1 Your first line is invalid JavaScript. You need to use single quotes or escape your inner double quotes. – trincot Commented Dec 16, 2016 at 22:24
2 Answers
Reset to default 4You have a syntax error in your str_arr.
var str_arr = '["myName","asldkfjs","2342","[email protected]","sdlkfjskldf",410]';
var arr = JSON.parse(str_arr);
You could try parsing it as JSON, because an array is valid JSON (assuming it uses double quotes for the strings).
arr = JSON.parse(str_arr);
Also, as @manonthemat mentioned, you need to either use single quotes to wrap the string literal where you declare str_arr (since it contains double quotes) or you need to escape the double quotes to avoid a syntax error.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744347191a4569755.html
评论列表(0条)