[英]Parse nested json in angular 4 (IONIC)
我需要在我的Ionic列表中解析嵌套的JSON。 我我需要得到這樣的東西
我正在使用IONIC和Angular 4。
我的JSON格式是:
{
"result": {
"engineering": [{
"name": "Tamil Nadu",
"colleges": {
"list": [{
"id": "1",
"title": "wdwd"
}, {
"id": "2",
"title": "titlealsadasbum2"
}]
}
}, {
"name": "Kerala",
"colleges": {
"list": [{
"id": "3",
"title": "titleqqwalbum2"
}, {
"id": "4",
"title": "titleaasalbum2"
}]
}
}],
"medicine": [{
"name": "Tamil Nadu",
"colleges": {
"list": [{
"id": "1",
"title": "med-wdwd"
}, {
"id": "2",
"title": "med-titlealsadasbum2"
}]
}
}, {
"name": "Kerala",
"colleges": {
"list": [{
"id": "3",
"title": "med-titleqqwalbum2"
}, {
"id": "4",
"title": "med-titleaasalbum2"
}]
}
}]
}
}
清單:
<ion-list padding>
<ion-list-header color="primary">TamilNadu</ion-list-header>
<ion-item>VIT UNIVERSITY CHENNAI </ion-item>
<ion-item>SRM UNIVERSITYCHENNAI</ion-item>
<ion-item>VELTECH UNIVERSITY</ion-item>
<ion-list-header> Kerala</ion-list-header>
<ion-item>HINDUSTAN UNIVERSITY</ion-item>
<ion-item>SRM UNIVERSITYCHENNAI</ion-item>
<ion-item>VELTECH UNIVERSITY</ion-item>
<ion-list-header> Karnataka</ion-list-header>
<ion-item>VIT UNIVERSITY CHENNAI </ion-item>
<ion-item>SRM UNIVERSITYCHENNAI</ion-item>
<ion-item>VELTECH UNIVERSITY</ion-item>
<ion-list-header>Andra Pradesh</ion-list-header>
<ion-item>VIT UNIVERSITY CHENNAI </ion-item>
<ion-item>SRM UNIVERSITYCHENNAI</ion-item>
<ion-item>VELTECH UNIVERSITY</ion-item>
</ion-list>
我正在使用功能獲取json:
getIndianCollegeSearchData() {
this.showLoader();
this.apiService.getCollegeData().then((result) => {
console.log(result);
this.loading.dismiss();
this.searchResults = result
this.items = Object.keys(this.searchResults.result);
}, (err) => {
this.loading.dismiss();
console.log(err);
});
在API控制器中:
getCollegeData(){
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('Authorization', this.authService.token);
this.http.get('http://13.126.17.194/colleges.php')
.map(res => res.json())
.subscribe(data => {
resolve(data);
}, (err) => {
reject(err);
});
});
}
如何在Angular中遍歷此JSON? 由於密鑰本身就是要打印的文本,因此我堅持解析此JSON。
我嘗試了這種方法:
<ion-list-header color="primary" *ngFor="let key of items">{{key}}</ion-list-header>
未捕獲(承諾):TypeError:無法讀取未定義的屬性“結果” TypeError:無法讀取未定義的屬性“結果”
更新:我添加了如下所述的對象。 但是如何在關鍵工程中獲得價值?
更新
如@JB Nizet
和@Sajeetharan
我將代碼更改為HttpClient,並在控制台中獲取了響應json。 但是當我將代碼添加到@Sajeetharan
提到的html中時, @Sajeetharan
了錯誤:
錯誤錯誤:未捕獲(承諾):TypeError:無法讀取未定義的屬性“結果” TypeError:無法讀取Object.eval上未定義的屬性“結果”(作為updateDirectives)(IndianstudiesPage.html:24)
正如@JB Nizet
在評論中提到的,使用HttpClient
代替Http
,這是示例代碼,
在您的服務中,
import {HttpClient, HttpHeaders} from '@angular/common/http';
private static defaultHeaders(): HttpHeaders {
return new HttpHeaders({'Content-Type' : 'application/json'});
}
constructor(
private httpClient: HttpClient
) {}
getCollegeData(): Observable<any> {
const headers: HttpHeaders = new Headers();
return this.httpClient.get<any>('http://13.126.17.194/colleges.php', {headers: headers.append('Authorization', this.authService.token)});
}
然后在您的組件內部
ngOnInit() {
this.apiService.getCollegeData().subscribe((colleges) => this.searchResults = colleges);
}
由於您具有嵌套數組,因此需要使用嵌套ngFor,因此將HTML更改為
<ion-list-header color="primary" *ngFor="let details of searchResults.result.engineering">
<ng-container *ngFor="let detail of details.colleges.list">
{{detail.title}}
</ng-container>
</ion-list-header>
如果發現一些問題,可能需要稍作更改。
終於建立了一個解決方案:
使用安全的導航器操作符:
<ion-list>
<ion-list-header *ngFor="let details of searchResults?.result?.engineering">
{{details.name}}
<ion-item no-lines *ngFor="let detail of details?.colleges" (click)="itemSelected(detail.id)" >{{detail.title}}</ion-item>
</ion-list-header>
</ion-list>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.