【问题标题】:How to convert a given string to object in AngularJS如何在AngularJS中将给定的字符串转换为对象
【发布时间】:2021-07-14 09:17:36
【问题描述】:

我想将字符串转换为 Object,下面是我的代码

$scope.myString = "{'profit': 2.276273727416992}";
$scope.result = angular.toJson($scope.myString);
console.log('is object = ' + angular.isObject($scope.result)); // false

我正在使用angular.toJson() 进行转换,但它无法正常工作。 请让我知道这里出了什么问题以及如何转换

【问题讨论】:

    标签: json angularjs tostring


    【解决方案1】:

    您需要使用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
    

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 2015-12-26
      • 2017-03-26
      • 1970-01-01
      • 2021-02-03
      • 2018-07-19
      相关资源
      最近更新 更多