transform string into javascript array

Asked

Viewed 599 times

1

Good afternoon,

have the following string

[{"programa":"teste"},{"programa":"Aprender"},{"programa":"outro teste","indice":"0;1;0"}][{"programa":"Programando 1","indice":"1;2;3"},{"programa":"hostórico","indice":"1;2;2"},{"programa":"análise","indice":"1;2;1"}]

how do I get array through that string?

  • There’s supposed to be two arrays on this JSON ? ...{"programa":"outro teste","indice":"0;1;0"}][{"programa":"Programando 1","indice":"1;2;3"},...

  • I actually used the Concat to unite

  • This string comes from where ? The format was supposed to be JSON ?

  • yes, it would be but as it is the junction ended up getting this format. but I will adjust the formatting. Obg

1 answer

3


Your string JSON appears to be poorly formatted, has two poorly defined arrays (this gives error: ][).

I made a small change to a valid array and converted to object using JSON.parse:

var x = '{"1":[{"programa":"teste"},{"programa":"Aprender"},{"programa":"outro teste","indice":"0;1;0"}],"2":[{"programa":"Programando 1","indice":"1;2;3"},{"programa":"hostórico","indice":"1;2;2"},{"programa":"análise","indice":"1;2;1"}]}';

var o = JSON.parse(x);
console.log(o);

  • 1

    If you use eval instead of JSON.parse, hence the javascript of the question is valid! But the result is undefined.

  • 1

    I hadn’t even thought about it, I thought of an array with an array inside. I tested it here and it actually stays undefined well-observed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.