2017-03-19 5 views
0

: 데이터를 가져 오기 위해이 내 코드입니다Angular2 HTTP GET 나는이 JSON 파일에서 "ID"요소의 단지 값 좀하고 싶습니다 첫 번째 요소

{ 
"data": { 
    "id": 2, 
    "first_name": "lucille", 
    "last_name": "bluth", 
    "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg" 
} 

을} :

getData() { 

    return this.http.get('https://reqres.in/api/users/2') 
    .map((respone: Response) => respone.json()); 
} 

어떻게 'id'값을 얻을 수 있습니까?

+0

관측 대상을 반환 하시겠습니까? – Aravind

답변

1

의 다양한 사용할 수 있습니다 서비스, 에서 관찰 가능한 반환된다고 가정하고, 그런 다음 데이터를 구문 분석하십시오.

this.getData().subscribe(result => { 

    // handle the response 

    return result.data.id; 

}); 
0

당신은 당신이 getData()를 호출 한 후, 당신은 결과에 가입해야합니다 RxJs를 사용하면 가정 RxJs 첫번째 운영자

this.serviceName.getData().first(x => console.log(x.id)) //logs the first id 
          .subscribe(data => console.log(data); // logs the stream of data 
0

뭔가처럼 당신도 할 수 있습니다 :

getData() { 
    return this.http.get('https://reqres.in/api/users/2') 
        .map((respone: Response) => respone.json().data.id); 
} 

대신 전체 데이터 객체의 응답에서만 id 필드를 반환합니다.