Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 cast string to JSON

Tags:

json

angular

What is the right syntax to cast a string to JSON in Angular2? I tried:

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

it says: someString.toJSON is not a function

I'm lost because it was working with Angular1.


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

var someString;
someString.att = 'test';

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

like image 824
F3L1X79 Avatar asked Nov 24 '16 14:11

F3L1X79


People also ask

How can we convert an object to JSON string in angular?

The angular. toJson() Function in AngularJS is used to serialize the javascript object into a JSON – formatted string. It takes the javascript object and returns a JSON string.

What is JSON parse in angular?

parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string.

What is JSON Stringify?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

How use JSON pipe in TS file?

JsonPipe uses json keyword to convert value into JSON string using pipe operator as follows. For the example find the object of Address class in our component. address1 = new Address('Dhananjaypur', 'Varanasi', 'India'); Use json keyword with pipe operator (|) to convert the given object into JSON string.

How to convert/parse JSON to/from a JavaScript object in Angular application?

How to Convert/parse JSON to/from a javascript object in Angular application. 1 JSON.stringify () method string version of an object, which is the conversion of an object to JSON string 2 JSON.parse () - parse string JSON object and creates javascript object More ...

What is @JSON pipe in angular?

JSON pipe is an angular inbuilt pipe. and output displayed is in the JSON format printed in the browser. It helps the developer to print JSON objects during debugging for any issues. Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever.

How to convert an object to a JSON string?

JSON.stringify () method string version of an object, which is the conversion of an object to JSON string The output you see in the browser and console. create a typescript interface that declares all the fields of JSON objects

How to parse JSON data in JavaScript?

How to parse JSON data. We have seen a simple example to convert data from an object to JSON. Now, if we have data into JSON format, then we need to convert it into object format. Here, one method will be useful, i.e., JSON.parse(). It takes the data in JSON format and converts it into JavaScript Object format.


2 Answers

Angular2 uses JavaScript functions unlike Angular1.

Angular1 implements its own functions which is a bad thing.

In Angular2 just use pure JavaScript.

var json = JSON.parse(string);
like image 87
danday74 Avatar answered Oct 30 '22 22:10

danday74


Try using JSON.parse()

var someString: string = "your JSON String here";
var jsonObject : any = JSON.parse(someString)
like image 31
hzitoun Avatar answered Oct 30 '22 21:10

hzitoun