角2观察到的JSON错误
问题描述:
import { Injectable } from '@angular/core';
import { Http,Response } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
@Injectable()
export class CommentService{
private _url :string ="https://jsonplaceholder.typicode.com/posts"
constructor(private _http:Http){}
// method to fetch CommentS from a api service
getComments(){
return this._http.get(this._url)
.map((response:Response)=> response.json())
.catch(this._errorHandler);
}
_errorHandler(error:Response){
console.error(error);
return Observable.throw(error ||"Server Error");
}
}
上面的代码的伟大工程这个网址https://jsonplaceholder.typicode.com/posts角2观察到的JSON错误
但不与这个网址http://ergast.com/api/f1/2016/driverStandings.json
任何工作思路... TIA
答
阿拉汶是在正确的轨道上在这里,但MRData
情况下敏感和映射是有点关闭。这里映射响应的正确方法应该是:
return this._http.get(this._url)
.map((response:Response)=> response.json().MRData)
.catch(this._errorHandler);
}
你的组件:
getData() {
this.service.getData()
.subscribe(data => {
this.data = data;
});
}
然后你就可以访问数据,例如像:
<div>
<b>Series:</b> {{data?.series}}<br>
<a><b>Url:</b> {{data?.url}}</a>
</div>
然后,你似乎有很多嵌套的对象在你的回应中,所以这可能会对你有所帮助:Access/process (nested) objects, arrays or JSON
这是一个演示程序,带有mo CK JSON,但我确实尝试了你提供的网址,并且数据收到的很好。所以复制plunker应该在你的应用程序正常工作:)
答
为了使第二您需要使用的一项工作来获取数据
return this._http.get(this._url)
.map((response:Response)=> response.MRDATA.json())
.catch(this._errorHandler);
}
是原因在此working one的数据是在对象数组类型含义
的其中一个does not work是完全单一的对象
+0
谢谢,我会回报 – user17970
你可以发布什么样的错误你好吗?我是你把https而不是http。我测试了第二个网址,它的工作原理。 – Coyote