如何在ListView angular2本机脚本中解析此json结构 [英] How to parse this json structure in listview angular2 nativescript
本文介绍了如何在ListView angular2本机脚本中解析此json结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
json结构:
{
"Employee": [
{"id":1,"name":"Dan" },
{"id":2,"name":"Stack" },
.....
]
}
app.component.ts:
ngOnInit() {
console.log("first", "Test");
this.globalReader.getObjectData()
.subscribe(data => this.getData = JSON.stringify(data), ---> Response printed successfully here.I'm able to print it in label.
error => alert(error),
() => console.log("finished")
);
}
组件:
<label text ="{{getData }}" ></label>
getObjectData() {
return this._http.get('/page/emp.json')
.map((response :Response) => response.json());
}
之后,我不知道如何解析json并在此结构的listview中打印出来.我提到了一些视频和杂货店应用程序.但是我仍然无法获得结果,我对arrayname Employee非常困惑.
After that I don't know how to parse the json and print it in listview for this structure. I referred some videos and grocery application. But I'm unable to get the result still.I'm very much confused with arrayname Employee.
我只需要从列表视图中的响应中打印name
.
I need to print only the name
from response in listview.
推荐答案
这应该有效:
<ListView [items]="employees" row="1">
<template let-item="item">
<Label [text]="item.name"></Label>
</template>
</ListView>
// Create Employee Class:
export class Employee {
constructor(public id: string, public name: string) {}
}
// Your component:
this.employees: Array<Employee> = [];
ngOnInit() {
this.globalReader.getObjectData()
.subscribe(data => {
this.employees = data.Employee.map(item => new Employee(item.id, item.name);
});
});
}
// The getObjectData method of *globalReader* service:
getObjectData() {
return this._http.get('/page/emp.json')
.map((response :Response) => response.json().data);
}
根据http调用返回的对象,可能不需要.data:
Depending on the object returned by the http call, the .data may not be required:
getObjectData() {
return this._http.get('/page/emp.json')
.map((response :Response) => response.json());
}
这篇关于如何在ListView angular2本机脚本中解析此json结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文