尽管从AngularJS documentation for angular.fromJson是壮观的,我仍然不知道如何充分利用它的潜力。最初,我刚刚直接将来自HTTP请求的JSON数据响应分配给$scope
变量。我最近注意到Angular有一个内置的fromJson()
函数,这似乎是我想要使用的东西。如果我使用它,是否更安全,我可以更容易地访问数据元素吗?JSON反序列化与angular.fromJson()
这是我怎么一直在做:
$http({
method: 'GET',
url: 'https://www.reddit.com/r/2007scape.json'
}).then(function success(response) {
var mainPost = response; // assigning JSON data here
return mainPost;
}, function error(response) {
console.log("No response");
});
这是我可以做的:
$http({
method: 'GET',
url: 'https://www.reddit.com/r/2007scape.json'
}).then(function success(response) {
var json = angular.fromJson(response); // assigning JSON data here
return json;
}, function error(response) {
console.log("No response");
});
'response'应该已经被解析为数组或对象..不是json字符串 – charlietfl