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:
JavaScript
x
14
14
1
$scope.serviceCalls = function() {
2
var serviceUrl = "http://localhost:8080/rest/1";
3
var promise = CommonService.getServiceJSON(serviceUrl);
4
promise.then(function(result) {
5
$scope.jsondata = result;
6
console.log($scope.jsondata); // getting the JSON in console logs
7
console.log($scope.jsondata.target); //returns undefined
8
}, function(reason) {
9
alert('Failed: ' + reason);
10
}, function(update) {
11
alert('Got notification: ' + update);
12
});
13
}
14
JSON response that I am receiving:
JavaScript
1
10
10
1
[{
2
"target": "xxxxxxxxxxxx",
3
"datapoints": [
4
[14711037952.0, 1474340220],
5
[14711058432.0, 1474340280],
6
[14719434752.0, 1474361700],
7
[null, 1474361760]
8
]
9
}]
10
Advertisement
Answer
Response is an array, so you have to use an index.
Example
JavaScript
1
2
1
console.log($scope.jsondata[0].target);
2