16

I have a string like :

$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';

and I want to convert to js object:

 $scope.tmp =  {"firstName":"John","age":454 };

Note: JSON.parse() doesn't work!!

It's my sample in codepen

Ehsan Ali
  • 1,362
  • 5
  • 25
  • 51

1 Answers1

24

You can do it with angular.fromJson()

in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);

The difference between JSON.Parse() and angular.fromJson, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66