2017-01-13 1 views
0

OK, 난 곤경에 빠졌습니다. 예상대로 결과를 가져 오는 각도 json GET 프로세스가 있지만 결과를 개별 값으로 파싱 할 수 없습니다. 여기 Angular JSON Parse

는 내가 갖는 문자열 결과 :

{"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]} 

그리고 여기 내 코드입니다 :

SO
$http({ 
method:'GET', 
url:$scope.url, 
transformResponse: undefined}).then(function(response) { 
    console.log(response.data); 
}).catch(function(response) { 
    console.error('Error', response.status, response.data); 
}) 
.finally(function() { 
    console.log("All Done"); 
}); 

, 어떻게이 sys_id의 개별 값을받을 수 있나요? 사전에

덕분에, DP

******* 편집 : Sajeetharan는 여전히 문제가 발생. 귀하의 예에 따라 변경된 코드 :

console.log(response.data); 
    $scope.data = response.data.result; 
    console.log($scope.data); 
    console.log("Sys id is:"+$scope.data[0].sys_id); 

출력 :

{"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]} 
undefined 
TypeError: Unable to get property '0' of undefined or null reference 
    at Anonymous function (******.EvScripts.jsdbx:59:3) 
    at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:130:399) 
    at m.prototype.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:96) 
    at m.prototype.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:142:165) 
    at m.prototype.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:399) 
    at l (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:97:248) 
    at K (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:101:373) 
    at y.onload (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:102:397) 
Error undefined undefined 
+0

귀하의 URL은 어디에 표시됩니까? 내 생각 엔 응답이 application/json content tyoe 헤더를 포함하지 않으므로 angullar가 자동으로 구문 분석하지 않습니다. –

+0

ServiceNow 인스턴스의 인시던트 테이블을 가리 킵니다. 헤더를 다음과 같이 설정했습니다. $ http.defaults.headers.common.Accept = "application/json"; –

+0

'Accept' 헤더는 서버 사용을위한 것입니다. 즉 클라이언트가 어떤 포맷을 사용할 수 있는지 말합니다. 서버는 적절한'Content-Type' 헤더로 응답해야합니다. 그래서 당신이 설정 한 헤더는 부적합합니다. –

답변

0

:

//CALL TO REST SERVICES 
//--------------------- 
$http({ 
method:'GET', 
url:$scope.url, 
transformResponse: undefined}).then(function(response) { 
    console.log(response.data); 
    $scope.data = angular.fromJson(response.data); 
    console.log($scope.data['result'][0].sys_id); 
}).catch(function(response) { 
    console.error('Error', response.status, response.data); 
}) 
.finally(function() { 
    console.log("All Done"); 
}); 
//--------------------- 
0

어레이 data0th index에 액세스 sys_id에 액세스하려면,

$http.get('test.json').then(function(response){ 
     $scope.data = response.data.result;  
    }); 

당신이 만약 표현식을 사용하여 sys_id에 액세스하려면

<div ng-controller="ListCtrl"> 
    <h1> Sys id is :{{data[0].sys_id}} </h1> 
    </div> 

당신이 컨트롤러를 사용하여 sys_id에 액세스하려면 HTML에서 6,

,이 angular.fromJson를 통해 작업 결국 무엇

$http.get('test.json').then(function(response){ 
      $scope.data = response.data.result; 
      console.log("Sys id is:"+$scope.data[0].sys_id);  
     }); 

DEMO