如何将给定字符串转换为 AngularJS 中的对象
How to convert a given string to object in AngularJS
我想将字符串转换为对象,下面是我的代码
$scope.myString = "{'profit': 2.276273727416992}";
$scope.result = angular.toJson($scope.myString);
console.log('is object = ' + angular.isObject($scope.result)); // false
我正在使用 angular.toJson()
进行转换,但没有成功。
请让我知道这里出了什么问题,我该如何转换
您需要使用 angular.fromJson
反序列化 JSON 字符串。 (link)
此外,json需要双引号:
$scope.myString = "{\"profit\": 2.276273727416992}";
$scope.result = angular.fromJson($scope.myString);
console.log('is object = ' + angular.isObject($scope.result)); // true
我想将字符串转换为对象,下面是我的代码
$scope.myString = "{'profit': 2.276273727416992}";
$scope.result = angular.toJson($scope.myString);
console.log('is object = ' + angular.isObject($scope.result)); // false
我正在使用 angular.toJson()
进行转换,但没有成功。
请让我知道这里出了什么问题,我该如何转换
您需要使用 angular.fromJson
反序列化 JSON 字符串。 (link)
此外,json需要双引号:
$scope.myString = "{\"profit\": 2.276273727416992}";
$scope.result = angular.fromJson($scope.myString);
console.log('is object = ' + angular.isObject($scope.result)); // true