Json 如何让Angular的HttpClient返回对象而不是字符串?
如何让HttpClient以JSON对象的形式返回数据 Angular文档说HttpClient应该将返回的JSON数据作为对象进行解析。在我的项目中,它只以字符串形式返回数据 如果我使用JSON.parse,我可以将数据转换成一个对象,并以这种方式获取数据,它就可以工作了。但从我在文章中看到的情况来看,这不是一个好的做法。此外,大多数文章/教程仅使用界面 我已经创建了一个界面并尝试使用它,但它不起作用。我读过很多帖子和文章,但是他们都说HttpClient默认情况下应该返回一个对象 这是我的服务代码。 第一个函数获取用户的坐标,第二个函数使用坐标从api请求数据Json 如何让Angular的HttpClient返回对象而不是字符串?,json,angular,typescript,Json,Angular,Typescript,如何让HttpClient以JSON对象的形式返回数据 Angular文档说HttpClient应该将返回的JSON数据作为对象进行解析。在我的项目中,它只以字符串形式返回数据 如果我使用JSON.parse,我可以将数据转换成一个对象,并以这种方式获取数据,它就可以工作了。但从我在文章中看到的情况来看,这不是一个好的做法。此外,大多数文章/教程仅使用界面 我已经创建了一个界面并尝试使用它,但它不起作用。我读过很多帖子和文章,但是他们都说HttpClient默认情况下应该返回一个对象 这是我的服
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, pipe } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from '../../../environments/environment';
import { Aqi } from './aqi';
@Injectable({
providedIn: 'root'
})
export class AqiService {
aqiUrlCoords = `${environment.apiUri}airvisual/geo/`;
constructor(private http: HttpClient) {}
getLocation(): Observable<any> {
return Observable.create(observer => {
if(window.navigator && window.navigator.geolocation) {
window.navigator.geolocation.getCurrentPosition(
(position) => {
observer.next(position);
observer.complete();
},
(error) => observer.error(error)
);
} else {
observer.error('Unsupported Browser');
}
});
}
getGeoLocationAqi(lat, long): Observable<any> {
return this.http.get<any>(this.aqiUrlCoords, { params: {
lat: lat,
long: long
}});
}
}
下面是使用服务和数据的组件。typeof总是打印字符串
import { Component, OnInit } from '@angular/core';
import { AqiService } from '../services/aqi/aqi.service';
import { Aqi } from './aqi';
export class GeoComponent implements OnInit {
aqi: any;
errorMessage: string;
constructor(private aqiService: AqiService) {}
ngOnInit() {
this.getCurrentCoordinatesAqi();
}
getCurrentCoordinatesAqi() {
this.aqiService.getLocation().subscribe(pos => {
let lat = pos.coords.latitude.toString();
let long = pos.coords.longitude.toString();
this.aqiService.getGeoLocationAqi(lat, long).subscribe((res) => {
this.aqi = res;
console.log(res);
console.log(typeof res);
});
}, (err) => {
this.errorMessage = err;
});
}
}
这是我制作的界面
export interface Aqi {
status: string,
data: {
city: string,
state: string,
country: string,
location: {
type: string,
coordinates: [
number,
number
]
},
current: {
weather: {
ts: string,
hu: number,
ic: string,
pr: number,
tp: number,
wd: number,
ws: number
},
pollution: {
ts: string,
aqius: number,
mainus: string,
aqicn: number,
maincn: string
}
}
}
}
下面是使用postman从api返回的内容
{
"status": "success",
"data": {
"city": "Hanoi",
"state": "Hanoi",
"country": "Vietnam",
"location": {
"type": "Point",
"coordinates": [
105.81881,
21.021938
]
},
"current": {
"weather": {
"ts": "2019-04-30T16:00:00.000Z",
"hu": 100,
"ic": "04n",
"pr": 1010,
"tp": 25,
"wd": 70,
"ws": 2.6
},
"pollution": {
"ts": "2019-04-30T15:00:00.000Z",
"aqius": 151,
"mainus": "p2",
"aqicn": 76,
"maincn": "p2"
}
}
}
}
我希望typeof打印对象,但除非我使用JSON.parse,否则它总是打印字符串。当我使用接口时,我仍然得到字符串。感谢您的帮助 您不需要解析响应,默认情况下,只要您的端点返回JSON对象,并且内容类型为application/JSON,http客户端就会返回对象。您的端点可能正在返回某些文本内容类型。这很有效!非常感谢你!我的api返回了一个字符串。我在后端使用JSON.parse让它发送一个对象。现在我可以在前端使用我的界面了。你可能应该为你的api使用rest框架,比如Nest.js或Express.js,而不是你自己的。你真的需要发布那么多代码吗?
{
"status": "success",
"data": {
"city": "Hanoi",
"state": "Hanoi",
"country": "Vietnam",
"location": {
"type": "Point",
"coordinates": [
105.81881,
21.021938
]
},
"current": {
"weather": {
"ts": "2019-04-30T16:00:00.000Z",
"hu": 100,
"ic": "04n",
"pr": 1010,
"tp": 25,
"wd": 70,
"ws": 2.6
},
"pollution": {
"ts": "2019-04-30T15:00:00.000Z",
"aqius": 151,
"mainus": "p2",
"aqicn": 76,
"maincn": "p2"
}
}
}
}