【问题标题】:Capture http json body response and convert into array in angular ionic5捕获http json body响应并转换为角度ionic5中的数组
【发布时间】:2021-05-17 16:29:10
【问题描述】:

我被卡住了,在查看这里和其他页面时,我找不到合适的东西:/ 我想要的是提取它并以一种我可以用来在 html 中构建一个简单表格的方式来获取它

response count
[value] [value]
[value] [value]

还可以将其设为列表格式,以便稍后在 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)
      )
  }

我的 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中添加?

非常感谢您的帮助。

【问题讨论】:

    标签: arrays json angular type-conversion converters


    【解决方案1】:

    改变这个

    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)
      )
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 1970-01-01
      • 2019-07-04
      • 2017-08-20
      • 2021-12-03
      • 2015-08-29
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多