Skip to content Skip to sidebar Skip to footer

Object Property Disappears When Converting To Json

Here's my class: export class Patient { constructor(public id: number, public name: string, public location: string, public bedId: number, public severity: string, public trajector

Solution 1:

From the JSON docs, JSON can't include functions so your method won't be parsed as JSON. Valid data types are string, number, object, array, true, false and null.

Here's an example of json.stringify() and json.parse() removing methods from an object:

var obj = {
	"prop" : "value",
    "method" : function( num ){
    	return num + 1;
    }
};

var json = JSON.stringify( obj );

var new_obj = JSON.parse( json );

Object.keys( new_obj ).forEach(function(key){
	console.log( key + " : " + new_obj[key]  );
});

Post a Comment for "Object Property Disappears When Converting To Json"