Angular 8 http.get 只绑定某个属性
Angular 8 http.get bind a certain property only
angular http.get 有没有办法在不需要根对象的情况下反序列化 json 对象?
json:
{
"jsonprop" : [
{
"id" : 1,
"name" : "testName"
},
{
"id" : 2,
"name" : "testName2"
}
]
}
型号:
export interface Jsonprop {
id: number;
name: string;
}
export interface RootObject {
jsonprop: Jsonprop[];
}
http 方法:(有效,但这不是我想要的)
getJsonProp(searchText: string): Observable<RootObject> {
return this.http.get<RootObject>('apiurl');
}
我想做的是:
getJsonProp(searchText: string): Observable<Jsonprop[]> {
return this.http.get<Jsonprop[]>('apiurl');
}
我有办法做到这一点吗?
您可以使用 RxJS 中的 map()
运算符。例如:
return this.http.get<RootObject>('apiurl').pipe(
map(response) => {
return response.jsonprop;
})
);
angular http.get 有没有办法在不需要根对象的情况下反序列化 json 对象?
json:
{
"jsonprop" : [
{
"id" : 1,
"name" : "testName"
},
{
"id" : 2,
"name" : "testName2"
}
]
}
型号:
export interface Jsonprop {
id: number;
name: string;
}
export interface RootObject {
jsonprop: Jsonprop[];
}
http 方法:(有效,但这不是我想要的)
getJsonProp(searchText: string): Observable<RootObject> {
return this.http.get<RootObject>('apiurl');
}
我想做的是:
getJsonProp(searchText: string): Observable<Jsonprop[]> {
return this.http.get<Jsonprop[]>('apiurl');
}
我有办法做到这一点吗?
您可以使用 RxJS 中的 map()
运算符。例如:
return this.http.get<RootObject>('apiurl').pipe(
map(response) => {
return response.jsonprop;
})
);