本文介绍了如何在listview angular2 nativescript中解析这个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 nativescript中解析这个json结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!