2017-02-27 5 views
0

非常に不愉快な形式でJSONを返すwagtail APIをクエリしようとしています。オブジェクトのインスタンシエーションで角度を無視した角度のオブジェクト

{ 
    "id": 3, 
    "meta": { 
     "type": "home.HomePage", 
     "detail_url": "http://localhost:8000/api/v1/pages/3/" 
    }, 
    "parent": null, 
    "title": "Homepage", 
    "body": "<h2>cool an h2 fgf</h2>", 
    "main_image": { 
     "id": 1, 
     "meta": { 
      "type": "wagtailimages.Image", 
      "detail_url": "http://localhost:8000/api/v1/images/1/" 
     } 
    }, 
    "header_image": { 
     "id": 1, 
     "meta": { 
      "type": "wagtailimages.Image", 
      "detail_url": "http://localhost:8000/api/v1/images/1/" 
     } 
    }, 
    "show_in_menus": true, 
    "full_url": "/media/images/Background-4.original.jpg" 
} 

私が本当に望むのは、このようなクラスです。

export class HomePage { 
    id: number; 
    title: string; 
    body: string; 
    full_url: string; 

} 

しかし、私は戻って私のサービスから戻ったデータから取得し、それを試してみて、ログインするたび、それは未定義です。

私はtypescriptのJSONから欲しくないフィールドを無視する方法はありますか?

私が使用しているサービスは以下のとおりです。

import { Injectable } from '@angular/core'; 
import {Http, Response} from '@angular/http'; 
import {Observable} from "rxjs"; 
import {HomePage} from "./HomePage"; 

@Injectable() 
export class HomePageService { 

    constructor(private http: Http){ 
    } 

    getHomePage(GUID: number): Observable<HomePage>{ 
    return this.http 
     .get("http://localhost:8000/api/v1/pages/" + GUID + "/") 
     .map(this.extractData) 
     .catch(this.handleError); 
    } 
    private extractData(res: Response) { 
    let body = res.json(); 
    return body.data || {} 
    } 
    private handleError (error: Response | any) { 
    // In a real world app, we might use a remote logging infrastructure 
    let errMsg: string; 
    if (error instanceof Response) { 
     const body = error.json() || ''; 
     const err = body.error || JSON.stringify(body); 
     errMsg = `${error.status} - ${error.statusText || ''} ${err}`; 
    } else { 
     errMsg = error.message ? error.message : error.toString(); 
    } 
    console.error(errMsg); 
    return Observable.throw(errMsg); 
    } 
} 

とコンポーネント:

import {Component, OnInit, OnDestroy} from '@angular/core'; 
import {HomePageService} from './home-page.service'; 
import {ActivatedRoute} from '@angular/router'; 
import {HomePage} from "./HomePage"; 

@Component({ 
    selector: 'app-home-page', 
    templateUrl: './home-page.component.html', 
    styleUrls: ['./home-page.component.css'], 
    providers: [HomePageService] 
}) 
export class HomePageComponent implements OnInit, OnDestroy{ 
    id: number; 
    private sub: any; 
    public homePage: HomePage; 
    errorMessage: string; 
    constructor(private homePageService : HomePageService, private route: ActivatedRoute) { 

    } 

    ngOnInit() { 
    this.sub = this.route.params.subscribe(params => { 
     this.id = +params['id']; 
    }); 
    this.homePageService.getHomePage(this.id) 
     .subscribe(
     homePage => this.homePage = new HomePage(homePage), 
     error => this.errorMessage = <any>error, 
     () => console.log(this.homePage.full_url) 
    ); 
    console.log(this.id); 
    } 
    ngOnDestroy() { 
    this.sub.unsubscribe(); 
    } 

} 
+0

データを取得しているサービスのコードを追加できますか? – eminlala

+0

コンポーネントとサービスコードを追加しました。ありがとうございます。 –

+0

'homePage => this.homePage =新しいHomePage(ホームページ)' - あなたは 'HomePage'クラスのコンストラクタを持っていますか? – eminlala

答えて

1

homePage => this.homePage = new HomePage(homePage) - あなたのコードでは、私はHomePageクラスに定義されたコンストラクタが表示されません。したがって、homePageオブジェクトを渡すと何も起こりません。これを試してください:

export class HomePage{ 
    id: number; 
    title: string; 
    body: string; 
    full_url: string; 

    constructor(homePageObj: any) 
    { 
     if (homePageObj) 
     { 
      this.id = homePageObj.id; 
      this.title = homePageObj.title; 
      this.body = homePageObj.body; 
      this.full_url = homePageObj.full_url; 
     } 
    } 
} 
+0

こんにちは、ありがとう、それは動作しませんでした。 homePageObjには何も入力されていません。正直言って、私はコンストラクタがこのようなコンストラクタを満たしていないHeroesの例のようにクラス変数を定義すると自動的に生成されると考えました。 –

+0

実際にこのサービスに問題がありました。 return body.data || {} は になる必要がありました return body || {} 私はどこか悪いところからそのコードをコピーしました。私はこれが私が尋ねた質問に答えると信じています。パラメータがレスポンスボディと正しく一致しない場合は、角度が必要になります。エラーが発生し、コンストラクタを手動で定義する必要があります。ありがとうございます –

+0

もう一度試すことができます 'return body.data || {} ' – eminlala

関連する問題