cannot access objects inside service response

Tags: , , , ,



When I tried to access the JSON response I cannot access the object.

I need to get the target and datapoint objects and after that I need to iterate the dataPoint array.

result.target is undefined in the above case.

Controller:

$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 response that I am receiving:

[{
    "target": "xxxxxxxxxxxx",
    "datapoints": [
        [14711037952.0, 1474340220],
        [14711058432.0, 1474340280],
        [14719434752.0, 1474361700],
        [null, 1474361760]
    ]
}]

Answer

Response is an array, so you have to use an index.

Example

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


Source: stackoverflow