JSON输入:
let url = this.common.apiURLs+"/sapp/viewq?id="+questionId+"&user_id="+val+"&format=json";
关注: JSON Data Link
编码
public mykey: any;
public allData: Array<string> = [];
public userId : any;
public qid : any;
public answerId: any;
public doctorId: any;
public rating: any;
public headers:any;
public ratStar:any;
public feedback:any;
public speciality:any;
public submitQid:any;
public unpaidFree: any;
public queryStatus: any;
public maxKey: any;
constructor(public common: CommonProvider, public navCtrl: NavController, public navParams: NavParams, public fb: FormBuilder, public http: Http, private storage: Storage, public loadingCtrl: LoadingController, public toastCtrl: ToastController, public alertCtrl: AlertController) {
/* Get From Another page */
this.qid = navParams.get('questionId');
this.queryStatus = navParams.get('qstatus');
this.viewQuery(this.qid).then(data => {
this.allData = data;
this.mykey = Object.keys(data);
this.mykey.forEach((key: any) => {
this.maxKey = key;
});
if(this.queryStatus=='unpaid'){
this.unpaidFree = this.allData[this.maxKey].unpaid['fee'];
console.log("Unpaid Free : "+this.unpaidFree);
}
});
}
/* View Query Api Call*/
viewQuery(questionId): Promise<any> {
return new Promise((resolve) => { this.storage.get('id').then((val) => {
let url = this.common.apiURLs+"/sapp/viewq?id="+questionId+"&user_id="+val+"&format=json";
this.http.get(url)
.map(res => res.json())
.subscribe(data => {
resolve(data);
});
});
});
}
我遇到以下错误:
this.unpaidFree = this.allData[this.maxKey].unpaid['fee'];
请解释我。
答案 0 :(得分:1)
尝试使用以下代码从JSON获取费用数据:
var lastObject = this.allData[this.maxKey];
var unpaid = lastObject['unpaid'];
this.unpaidFree = unpaid['fee'];
使用此功能从JSON获取您的价值,希望这会有所帮助。