当我尝试访问JSON响应时,我无法访问该对象。我需要target
和datapoint
对象,然后我需要迭代dataPoint
数组。无法访问服务响应中的对象
result.target
在上述情况下未定义。
控制器:
$scope.serviceCalls = function() {
var serviceUrl = "http://localhost:8080/rest/1";
var promise = CommonService.getServiceJSON(serviceUrl);
promise.then(function(result) {
$scope.jsondata = result;
console.log($scope.jsondata); // getting the JSON in console logs
console.log($scope.jsondata.target); //returns undefined
}, function(reason) {
alert('Failed: ' + reason);
}, function(update) {
alert('Got notification: ' + update);
});
}
JSON响应,我接受:
[{
"target": "xxxxxxxxxxxx",
"datapoints": [
[14711037952.0, 1474340220],
[14711058432.0, 1474340280],
[14719434752.0, 1474361700],
[null, 1474361760]
]
}]
'$ scope.jsondata [0] .target' - 结果是一个数组。还要确保结果是Json而不是字符串(否则使用JSON.parse(result)来解析它) – mkaran
谢谢。 我错过了其数组JSON的事实。 –