Angular 6错误:我从api获取的数据是字符串格式,如何解析?

时间:2018-09-24 07:16:24

标签: json angular angular6

hi要显示从我的api到前端(Angular 6)的数据,我正在使用Angular 6的HttpClient方法我对Angular还是陌生的 我从api获取的数据是字符串格式,我需要解析,下面是响应图像

the data which I am getting from api is in the string format, I need to parse, below is the response image

这是model.ts

SELECT start_date + gs * interval '10 hours'
FROM (
    SELECT '2008-03-01 00:00:00'::timestamp as start_date, generate_series(1,10, 1) as gs
) s

这是组件

generate_series

这是服务

export interface Events {

IE_Incident_Start_Time: string;
IE_Start_time: string;
Title: string;
IE_Start_By: string;
Domain: string;
Impact: string;
IE_BU_Description: string;
}

html视图

enter code here
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Incident } from '../../shared/incidents.model';
import { DataStorageService } from '../../shared/data-storage.service';

@Component({
selector: 'app-active-incident',
templateUrl: './active-incident.component.html',
styleUrls: ['./active-incident.component.css']
})
export class ActiveIncidentComponent implements OnInit {

incidents: Events[];

constructor(private router: Router, private dataStorageService: 
DataStorageService) { }

ngOnInit() {
this.dataStorageService.getIncidents()
.subscribe(
(data: Events[]) => this.incidents = data,
(err: any) => console.log(err),
() => console.log('All done getting incidents')
);
}

2 个答案:

答案 0 :(得分:0)

是的,您可以这样做。

    this.dataStorageService.getIncidents().subscribe((data: Events[]) =>{
    data.foreach(()=>{
 this.incidents.push(JSON.parse(data));
       })

    },
    (err: any) => console.log(err),
    () => console.log('All done getting incidents')
    ); 

答案 1 :(得分:0)

您也可以使用这种方式

return this.dataStorageService.getIncidents()
      .pipe(
        map(jsonObj => Object.assign(new Events(), jsonObj),
        catchError(error => console.log('Error!'))
      );