[英]Return a HashMap from HTTP GET using angular 10
我正在使用以下版本。 Angular CLI:10.0.1
節點:12.18.2
操作系統:win32 x64
Angular:10.0.2
我有一個 Java spring 引導服務(運行良好),它將數據作為 HashMap 返回。
Map<String, List<String>>
以下是服務調用的output:
// 20200728103825
// http://localhost:1234/config-data
{
"books": [
"ABC",
"PQR",
"XYZ",
"QWT",
"LMN",
],
"categories": [
"FICTION",
"DRAMA"
]
}
我還有一個簡單的調用,它返回書籍列表(作為字符串)。 例如
// 20200728112259
// http://localhost:1234/book-data
[
"ABC",
"PQR",
"XYZ",
"QWT",
"LMN",
]
現在,我想在我的 Angular 代碼中使用它。
我的服務定義如下。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(
private http: HttpClient
) { }
loadBookData(){
console.log("loadBookData Service");
return this.http.get<string[]>('http://localhost:1234/book-data');
}
loadConfigData() {
console.log("loadConfigData Service");
// Looking for help here
}
}
基本上我能夠閱讀書籍數據(字符串 [])。
但是,應該如何從服務中讀取復雜數據,即 HashMap?
您可以像這樣聲明 model :
export class Result{
books: string[];
categories: string[];
}
您的 loadConfigData 應如下所示:
loadConfigData() {
console.log("loadConfigData Service");
return this.http.get<Result>('http://localhost:1234/config-data')
.pipe(
map((data:Result) => {
console.log(data.books);
console.log(data.categories);
})
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.