最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论