P
P
pandaa2020-05-20 12:47:59
JavaScript
pandaa, 2020-05-20 12:47:59

How to parse a string as an array inside json?

const req = {array: '[{num: 1}, {num: 2}, {num: 3}]'};
console.log(req.array) //Выводит как строку, а нужно вывести как массив.
console.log(JSON.parse(req.array)) // Не работает

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan Krapivin, 2020-05-20
@pandaa

console.log(JSON.parse(req.array)) // Не работает
Of course, it just takes it and "does not work" - it probably has a day off ...
This code should give an error. Have you tried reading the error?

'[{num: 1}, {num: 2}, {num: 3}]'

if it was JSON it would work. But it's not JSON
in JSON format, field names must be in double quotes:
'[{"num": 1}, {"num": 2}, {"num": 3}]'

E
Eugene, 2020-05-20
@Nc_Soft

Of course, they will shower me with pissing rags, but you can do it like this :D

const req = {array: '[{num: 1}, {num: 2}, {num: 3}]'};
const array = eval(req.array);
console.log(array);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question