Angular2 pour string in JSON

What is the correct syntax for translating a string in JSON in Angular2? I tried:

var someString; someString.toJSON(); //or someString.toJson(); 

he says: someString.toJSON is not a function

I got lost because I worked with Angular1.


If I try to add an attribute directly to my string (which is formatted as true JSON):

 var someString; someString.att = 'test'; 

he says: TypeError: Cannot create property 'att' on string '...'

+6
source share
2 answers

Angular2 uses JavaScript functions, unlike Angular1.

Angular1 implements its own functions, which is bad.

In Angular2, just use pure JavaScript.

 var json = JSON.parse(string); 
+17
source

Try using JSON.parse()

 var someString: string = "your JSON String here"; var jsonObject : any = JSON.parse(someString) 
+2
source

Source: https://habr.com/ru/post/1012636/


All Articles