我有一个字符串,类似于:
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
我想将其转换为js对象:
$scope.tmp = {"firstName":"John","age":454 };
注意:
JSON.parse()
无法使用!! 这是我在 codepen 中的示例。我有一个字符串,类似于:
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
我想将其转换为js对象:
$scope.tmp = {"firstName":"John","age":454 };
JSON.parse()
无法使用!! 这是我在 codepen 中的示例。您可以使用angular.fromJson()来完成。
在您的示例中,应为$scope.tmp = angular.fromJson($scope.text);
JSON.Parse()
和angular.fromJson
之间的区别在于,AngularJS将检查是否提供了一个字符串。如果已经是对象,则返回相同的对象。
$scope.text = "{\"firstName\":\"John\",\"age\":454 }";
- Deblaton Jean-Philippe
JSON.parse()
是我认为你正在寻找的。此外,这是一个重复的问题。 - evolutionxboxJSON.parse()
不起作用。 - Ehsan AliJSON.parse
就可以工作了。 - evolutionxbox