2016-09-20 47 views
0

当我尝试访问JSON响应时,我无法访问该对象。我需要targetdatapoint对象,然后我需要迭代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] 
    ] 
}] 
+2

'$ scope.jsondata [0] .target' - 结果是一个数组。还要确保结果是Json而不是字符串(否则使用JSON.parse(result)来解析它) – mkaran

+0

谢谢。 我错过了其数组JSON的事实。 –

回答

0

响应是一个数组,所以你必须使用一个索引。

console.log($scope.jsondata[0].target); 
0

尝试$scope.jsondata = JSON.parse(result);

0

你有什么在$ scope.jsondata?尝试解析结果。

0

$ scope.jsondata是一个数组

$scope.jsondata[0].target will give you target 
$scope.jsondata[0].datapoints will give you required array