简体   繁体   中英

Angular 6 Convert Json data to an Object

在此处输入图片说明

the data im getting from server comes in this format i want it to come in a form of an Object.

 public getOrder(): Observable < ORDERS > { return this._http.get < ORDERS > (`${this._apiBase}/charts/list/ORDERS/`); } 

here how im getting the data from server.

 ngOnInit() { this._dashService.getOrder().subscribe(order => { this.orders = order; console.log(this.orders); }) } 

This probably means that the your service is not setting the Content-Type property of the http header to application/json .

If you own the service, you could try to set the header property of the response. If you are not able to change it, you can try the JSON.parse() method.

This will do the trick!

 public getOrder(): Observable < ORDERS > { return this._http.get(`${this._apiBase}/charts/list/ORDERS/`).map((res) => <ORDERS>res.json()) } 

Just convert your response using json method.

public getOrder(): Observable < ORDERS > {
  return this._http.get(`${this._apiBase}/charts/list/ORDERS/`)
                   .map((res) =>res.results as ORDERS[] || [])
                   .catch((error:any) => Observable.throw(error.json().error));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM