[英]Unable to parse particular JSON data in AngularJS
This is a question I truly feel stupid for asking. 我真的很愚蠢地问这个问题。 But I have searched the web and haven't found a specific example with my problem.
但是我已经在网上搜索了,但是没有找到关于我的问题的具体示例。 Luckily, the question is simple.
幸运的是,这个问题很简单。 I have the following JSON data being read in by AngularJS as follows:
我有以下AngularJS读取的JSON数据,如下所示:
return $http.post('getData.htm').then(function(response) {
console.log("response.data: " + response.data);
var roles = angular.fromJson(response.data).model.results;
return roles;
});
The console in this scenario outputs the following: 在这种情况下,控制台输出以下内容:
response.data: {"model":{"results":["1","2","3","4","5","6","7","8","9","10","11"],"totalCount":11}}
I expect roles
to contain an array with those numbers, however when I print out the value of roles
it simply says [object Object]
. 我希望
roles
包含一个包含这些数字的数组,但是当我打印出roles
的值时,它只会简单地说[object Object]
。
How do I access the numbers contained in the results
array? 如何访问
results
数组中包含的数字?
thanks a lot! 非常感谢!
A javascript Array is also an Object so maybe when you did var roles = angular.fromJson(response.data).model.results;
一个javascript数组也是一个Object,所以也许当您做
var roles = angular.fromJson(response.data).model.results;
you got the array you wanted. 您得到了想要的阵列。 Try roles[1]
尝试角色[1]
Don't try to "load from JSON" a JSON object. 不要尝试“从JSON加载” JSON对象。
response.data
is already transformed in JSON format by angular.. Just use it like that : response.data
已经通过Angular转换为JSON格式。
console.log(response.data.model);
console.log(response.data.model.results);
console.log(response.data.model.results[1]);
http://jsbin.com/duruy/1/edit http://jsbin.com/duruy/1/edit
app.controller('firstCtrl', function($scope, $http){
activate();
$scope.roles = [];
function activate ()
{
return $http.get('http://jsbin.com/yewefo/1').then(function(response) {
console.log(response.data.model);
angular.copy(response.data.model.results, $scope.roles);
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.