javascript - What is ".items" and ".json()" - Stack Overflow

I am reading ng-book about Angular 2, there is a piece of code:return this.http.get(queryUrl).map((resp

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745451335a4628284.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信