Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON array from string

I want to convert the following string to an array

var string = '["YES","NO"]';

How do I do this?

like image 798
Sriya Avatar asked Mar 10 '26 13:03

Sriya


2 Answers

use the global JSON.parse method

JSON.parse('["YES","NO"]'); // returns ["YES", "NO"]

You can also use the JSON.stringify method to write the array back to a string if thats how you are storing it.

JSON.stringify(["YES", "NO"]); // returns '["YES", "NO"]'
like image 96
t3dodson Avatar answered Mar 12 '26 02:03

t3dodson


var str= '["YES","NO"]';
var replace= str.replace(/[\[\]]/g,'');
var array = replace.split(',');

Fiddle : http://jsfiddle.net/9amstq41/

like image 23
Thibault Bach Avatar answered Mar 12 '26 01:03

Thibault Bach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!