簡體   English   中英

捕獲 http json 身體響應並轉換為 angular ionic5 中的數組

[英]Capture http json body response and convert into array in angular ionic5

我被卡住了,在環顧這里和其他頁面時,我找不到合適的東西:/我想要的是提取它並以一種我可以用來在 html 中構建一個簡單表格的方式擁有它,看起來像

回復 數數
[價值] [價值]
[價值] [價值]

並以列表格式將其用於稍后在 chart.js 中使用,但這將在以后使用。

(僅供參考,我不是開發人員,一路學習——但我擅長樂高:)所以連接積木和適應是我學習編碼的方式,所以如果初學者有問題,我深表歉意:))

所以,我有一個 http 調用(GET)工作。 它返回正文:

[
  {
    "response": "Yes",
    "count": 1
  },
  {
    "response": "No",
    "count": 1
  }
]

但我不知道如何使用它......

我的 http 服務查詢:

getPollResults(pollId): Observable<Pollresults>{
      return this.http
      .get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
      .pipe(
        retry(2),
        catchError(this.handleError)
      )
  }

我的 model 用於 Pollresults:

export class Pollresults {

    response: string;
    count: number;

}

我的組件 page.ts 調用似乎捕獲了數據,但后來我被困在如何使用它上......

 export class PollResultsPage implements OnInit {

   uid: string;
   dataPollresults: Pollresults;

   constructor(
     private httpConfigService: HttpConfigService
   ) { }

   ngOnInit() {
      // this.id = this.activatedRoute.snapshot.params["id"];
      this.uid = "alphapoll";
      //get item details using id
      this.httpConfigService.getPollResults(this.uid).subscribe(response => {
      console.log(response);
      this.dataPollresults = response;
      //data => resolve(Object.keys(data).map(key => data[key]))
         })
   }

有關如何正確提取和獲取數組的任何幫助? 以及如何將其添加到 html 中?

非常感謝你的幫助。

改變這個

getPollResults(pollId): Observable<Pollresults>{
  return this.http
  .get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
  .pipe(
    retry(2),
    catchError(this.handleError)
  )

}

對此

getPollResults(pollId): Observable<Pollresults[]>{
  return this.http
  .get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
  .pipe(
    retry(2),
    catchError(this.handleError)
  )

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM