To parse it you need to use double quotes instead of single.
This should work:
services = '["service1", "service2", "service3"]'
JSON.parse(services)
Answer from Erick R Soto on Stack OverflowTo parse it you need to use double quotes instead of single.
This should work:
services = '["service1", "service2", "service3"]'
JSON.parse(services)
This is used to convert string which is array into pure array
var a = '[Aakash,akash]'
a.replace(/\[|\]/g,'').split(',')
(2) ["Aakash", "akash"]
Convert string into an array of arrays in javascript - Stack Overflow
javascript - how to convert an array string to an array - Stack Overflow
JavaScript - Converting string-array to an array of strings - Stack Overflow
javascript - Convert string with commas to array - Stack Overflow
The first thing you should try and do is to make my_string a valid JSON string. That means that instead of single quotes (') you have double quotes (") around your strings. To create a JSON string like this, you can use JSON.stringify() on the data you want to be converted into a string. Below is a valid JSON string of your array which you can use JSON.parse(my_string) on:
const my_string = '[["page1",5],["page2",3],["page3",8]]';
const res = JSON.parse(my_string);
console.log(res);
If you can't do this for whatever reason, then you can try one of the below options.
You can use JSON.parse() and .replace() to make your string a valid JSON string that is parseable like so:
const my_string = "['page1',5],['page2',3],['page3',8]",
stringified = '['+my_string.replace(/'/g, '"')+']';
console.log(JSON.parse(stringified));
Or you can use a Function constructor to "loosly" parse your JSON. Note that you should only use this if my_string is coming from a trusted source (ie: not user input):
const my_string = "['page1',5],['page2',3],['page3',8]";
arr = Function('return [' + my_string + ']')();
console.log(arr);
You can use the eval() function to convert your string to an array. See the code below.
my_string = "['page1',5],['page2',3],['page3',8]";
my_array = eval(`[${my_string}]`);
console.log(my_array);
However, using the eval() function comes with a set of drawbacks if not used properly. Please read through this answer before using eval in any of your serious code.
You can use use the JavaScript JSON.parse function to turn the string into a literal data type:
JSON.parse("{ key: 'value' }");
Or, you can choose to use Response.json instead of text() so that you retrieve an object response.
Right now you are using text() which is asking fetch to read the response and resolve it as a string.
See: https://developer.mozilla.org/en-US/docs/Web/API/Response/text
What you want is to replace .text() with .json()
See: https://developer.mozilla.org/en-US/docs/Web/API/Response/json
This should work:
fetch("./php/getuser.php", { method: "GET" })
.then((res) => res.json())
.then((data) => {
console.log(data);
});
JSON.parse would work, if the JSON wasn't invalid. As mentioned in the comments, the string would have to be formatted as '["1", "2"]'.
If you do not have control over the formatting, you can parse it manually: if the strings don't contain quotes, you can use #replaceAll("'", '"').
If you have edge cases you need to cover, json5 may be able to help: JSON5.parse(str) once you have the script loaded via NPM or unpkg
JSON only recognizes the usage of " ". To parse it you need to use double quotes instead of single quotes.
const myString = "['one', 'two']";
// change to
const myString = '["one", "two"]';
Heres an example:
// Not working
const Str = "['one', 'two']";
console.log(Str); // ['one', 'two']
console.log(typeof Str); // string
JSON.parse(Str); // SyntaxError: Unexpected token ' in JSON at position 1
// Working example:
const myString = '["one", "two"]';
console.log(myString); // ["one", "two"]
console.log(typeof myString); // string
const myArray = JSON.parse(myString);
console.log(myArray); // [ 'one', 'two' ]
console.log(typeof myArray); // object
In the case that you need to change ' to " just use replace.
For simple array members like that, you can use JSON.parse.
var array = JSON.parse("[" + string + "]");
This gives you an Array of numbers.
[0, 1]
If you use .split(), you'll end up with an Array of strings.
["0", "1"]
Just be aware that JSON.parse will limit you to the supported data types. If you need values like undefined or functions, you'd need to use eval(), or a JavaScript parser.
If you want to use .split(), but you also want an Array of Numbers, you could use Array.prototype.map, though you'd need to shim it for IE8 and lower or just write a traditional loop.
var array = string.split(",").map(Number);
Split it on the , character;
var string = "0,1";
var array = string.split(",");
alert(array[0]);