admin管理员组

文章数量:1425760

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.

Share Improve this question edited Jan 15, 2016 at 15:54 EricC asked Jan 15, 2016 at 15:46 EricCEricC 5,87015 gold badges54 silver badges73 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The 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

本文标签: javascriptWhat is quotitemsquot and quotjson()quotStack Overflow