抓取JSON并将其映射到对象
我想知道如何从我在线托管的链接中获取JSON数据。我试图在服务中获得lat和lng坐标,然后在我的组件中使用它们 JSON格式类似于:抓取JSON并将其映射到对象,json,angular,rxjs,ngrx,angular-httpclient,Json,Angular,Rxjs,Ngrx,Angular Httpclient,我想知道如何从我在线托管的链接中获取JSON数据。我试图在服务中获得lat和lng坐标,然后在我的组件中使用它们 JSON格式类似于: private busFeed = 'azure.com/file.json'; constructor(private _http: HttpClient) {} getFirstDevice() { return this._http.get(this.busFeed).pipe( map(data => dat
private busFeed = 'azure.com/file.json';
constructor(private _http: HttpClient) {}
getFirstDevice() {
return this._http.get(this.busFeed).pipe(
map(data => data),
tap(data => console.log('Device Info: ' + JSON.stringify(data)))
);
}
“设备”{[
“lat”:1234567
“lon”:1234567
]}
我试图像这样映射JSON对象:
private busFeed = 'azure.com/file.json';
constructor(private _http: HttpClient) {}
getFirstDevice() {
return this._http.get(this.busFeed).pipe(
map(data => data),
tap(data => console.log('Device Info: ' + JSON.stringify(data)))
);
}
我的TransportComponent是这样的:
this._transportFeed.getFirstDevice()
.subscribe(transportFeed => {
this.devices = transportFeed;
});
我面临的问题是,当我只需要纬度和经度坐标时,这是从JSON文档中获取所有信息。我希望在.ts文件中显示,而不是在.html文件中显示,因为我希望在地图的地图标记中绘制这些坐标
this._transportFeed.getFirstDevice()
.subscribe(transportFeed => {
this.devices = transportFeed.map(obj => ({
lat: transportFeed.lat,
lon: transportFeed.lon
})
});
或
谢谢你的快速回复!尝试这两种方法时收到的错误是:类型“Object”上不存在属性“lat”,类型“Object”上不存在属性“lon”。这是什么类型的设备?尝试将其更改为“any”。我将其更改为此。设备={lat:transportFeed[0]。lat,lon:transportFeed[0]。lon};现在我有一个错误,无法读取未定义的属性'lat',该属性不是有效的JSON。你能重新检查你的JSON格式吗?有关有效JSON格式的更多信息,请参见: