I am reading ng-book about Angular 2, there is a piece of code:
return this.http.get(queryUrl)
.map((response: Response) => {
return (<any>response.json()).items.map(item => {
// console.log("raw item", item); // unment if you want to debug
return new SearchResult({
id: item.id.videoId,
title: item.snippet.title,
description: item.snippet.description,
thumbnailUrl: item.snippet.thumbnails.high.url
});
});
});
What is .json()
in line 3? I have googled around, but cannot find any description for this method.
I am reading ng-book about Angular 2, there is a piece of code:
return this.http.get(queryUrl)
.map((response: Response) => {
return (<any>response.json()).items.map(item => {
// console.log("raw item", item); // unment if you want to debug
return new SearchResult({
id: item.id.videoId,
title: item.snippet.title,
description: item.snippet.description,
thumbnailUrl: item.snippet.thumbnails.high.url
});
});
});
What is .json()
in line 3? I have googled around, but cannot find any description for this method.
1 Answer
Reset to default 8The Http
object you are using returns an Observable<Response>
object whenever you call any request, in this case, a get
.
The Response
class has method called .json()
which attempts to return the body of the Response
object as a parsed JSON
object in order to make it easier to work with it.
The items
is just assuming that the response body has an items property, so the get
he's doing is expecting something like this to be returned:
{ items: ... }
Take a look at: https://angular.io/docs/ts/latest/api/http/Http-class.html
and https://angular.io/docs/ts/latest/api/http/Response-class.html