2017-08-16 48 views
1

的關鍵,我使用維基百科的API響應處理對象是這樣的:與和未知數量

{ 
'query': { 
      'pages': { 
        212187: { 
          'extract': value; 
          } 
        } 
      } 
} 

我想獲得提取物的價值。但是,當我無法通過212187時,因爲它是一個隨着我打電話而改變的數字。這不允許我準確地建模響應對象。 我目前的模型在212187級別是未定義的,阻止我進行提取。

我該如何做到這一點?任何幫助/建議/提示將不勝感激。

UPDATE 我有響應結構完全一致,除了212187我具有如數(假設是錯誤的)的接口。 我的要求是這樣的:

fetchDescriptionWiki(qSearch: string) { 
    const params: URLSearchParams = new URLSearchParams(); 
     params.set('origin', '*'); 
     params.set('format', 'json'); 
     params.set('action', 'query'); 
     params.set('prop', 'extracts'); 
     params.set('exintro', ''); 
     params.set('explaintext', ''); 
     params.set('indexpageids', ''); 
     params.set('titles', qSearch); 
    const options = new RequestOptions({ 
     search: params 
    }); 
    return this.http.get(this.pendingCardsDescriptionWikiUrl, options) 
     .map(this.extractApiData); 
    } 

extractApiData:

private extractApiData(res: Response) { 
    const body = res.json(); 
    return body || []; 
    } 

我component.ts功能看起來像:

getDescription() { 
    this.pendingCardService.fetchDescriptionWiki('SomeStringValue') 
    .subscribe(data => { 
     this.wikiResult = data; 
    }); 
} 

這是我被困具有能夠挖掘到反應並獲得提取物的價值。

+1

你的意思是在*類型看*?你需要像'{[key:number]:Extract}''。 – jonrsharpe

+0

您可以添加一些您當前使用的代碼來處理這個數據結構嗎? – DeborahK

+0

你介意擴大你的解釋@jonrsharpe嗎?你的意思是在界面上? –

回答

3

我已經使用了以下內容從維基百科API檢索數據使用Angular HttpClient(Angular 4+)提取查詢。它利用Object.keys(),然後通過數組索引(在這種情況下索引爲0)定位到所需的頁面,因此您不需要知道頁面標識。

我已經更新了您的示例中使用類和方法名稱的答案。我建議升級到HttpClient,但是您可以通過Angular 2的Http獲得相同的結果。

@Injectable() 
export class PendingCardService { 

    private: string wikipediaUrl = 
     'https://en.wikipedia.org/w/api.php?action=query' + 
     '&format=json&prop=extracts&exintro=1&explaintext=1' + 
     '&origin=*&titles=Some_title'; 

    fetchDescriptionWiki(): Observable<any> { 
     // this can use URLSearchParams as you did 
     return this.http.get(this.wikipediaUrl).map(this.extractApiData); 
    } 

    private extractApiData(res: Response): Foobar { 
     // json() is unnecessary in Angular 4+ as json() is automatically called 
     const body = res.json(); 

     const pages = body.query.pages; 
     const pageKeys = Object.keys(pages); 

     // Retrieve the first page (index 0) 
     const page = pages[pageKeys[0]]; 

     // ES6 object destructuring to get extract value 
     const { extract } = page; 

     // creating some object from retrieved value 
     const foobar: Foobar = { extract }; 

     return foobar; 
    } 
} 

組件:

getDescription() { 
    this.pendingCardService.fetchDescriptionWiki() 
    .subscribe((foobar: Foobar) => { 
     this.wikiResult = foobar; 
    }); 
} 

希望幫助!

+0

我沒有意識到你正在尋找模型定義而不是提取數據的過程,希望它仍然以某種方式提供幫助。謝謝! –

+0

老實說,我正在尋找兩個。試圖獲取提取值。我還沒有更新到4.3.4,所以我沒有花時間與HttpClient。我會仔細看看的。謝謝! –

+0

更新了使用您提供的類/方法名稱的答案。在調用json()之後,你應該可以通過Object.keys()來訪問'extract'屬性。我使用這種方法(Angular 4+)專門針對單個/特定頁面標題獲取'extract'屬性。理想情況下,您可以訪問'map()'執行的'extractApiData()'中的值。 –

0

像這樣:

interface IWikipediaData { 
    query: { 
     pages: { 
      [key: number]: { 
       extract: string 
      } 
     } 
    } 
} 

let example: IWikipediaData = { 
    query: { 
     pages: { 
      212187: { 
       extract: 'value' 
      } 
     } 
    } 
} 
+0

我認爲DeborahK需要知道頁碼以獲取提取值。將有助於實現setter自定義實現,重新格式化響應以獲得更開發的firendly對象。 –