我收到有效JSON上的JSON.parse错误

时间:2014-02-04 20:49:23

标签: json angularjs

我正在解析一些JSON。我的JSON是有效的,因为当我通过JSONLint运行它时,我得到了有效JSON的绿色标签,但由于某种原因,我仍然无法通过我的Angular Controller解析JSON。

JSON代码可以是found here

控制器代码:

savvyApp.controller('ProductsCtrl', function($scope) {

    var apiJSONResult = '<linked json here>';   

    $scope.apiResult = JSON.parse(apiJSONResult);

});

2 个答案:

答案 0 :(得分:2)

JSON.parse string with quotes

这应该是答案。简而言之,您不能只复制粘贴JSON并将其引用单引号并期望它能够正常工作。您还需要确保反斜杠已编码。

因此JSON 实际上是有效的。但是,如果将其粘贴到js文件中,它将不再有效。它与javascript如何编码字符串中的反斜杠有关。

答案 1 :(得分:0)

尝试使用angular fromJson方法:

&#13;
&#13;
savvyApp.controller('ProductsCtrl', function($scope) {

    var apiJSONResult = '<linked json here>';   

    $scope.apiResult = angular.fromJson(apiJSONResult);

});
&#13;
&#13;
&#13;

我也在这里留下方法

的Angular文档链接

https://docs.angularjs.org/api/ng/function/angular.fromJson