2016-09-20 5 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 무엇일까요 $scope.jsondata = JSON.parse(result);

0

를 시도? 결과를 파싱 해보십시오.

0

$의 scope.jsondata는

$scope.jsondata[0].target will give you target 
$scope.jsondata[0].datapoints will give you required array 
관련 문제