我正在用angular 2开发一个应用程序。我已经创建了一个服务,现在我想从json数据中获取特定数组,我该如何获取。
我有这样的服务代码
getProjects(): Promise<Project>{
return this.getData()
.toPromise()
.then(response => response.json())
.catch((error: string) => console.log(error));
}
这将返回所有json数据,但我只希望从json中提取“ Projects”数组。
JSON代码
{
"features":[],
"posts":{},
"Projects":[
{
"id":"proj-1",
"name":"Design for creative",
"meta":{
"date":"03 November, 2016",
"url":"http://www.pixelsocket.com",
"skills":[
"ui design",
"programming",
"marketing"
]
},
"images":[
"assets/images/design-creative-01.jpg",
"assets/images/design-creative-02.jpg",
"assets/images/design-creative-03.jpg",
"assets/images/design-creative-04.jpg"
],
...
}
更新
getProject(id: string): Promise<Project>{
const url = `${this.getProjects()}/${id}`; // can i call getProjects() method here
return this.http.get(url)
.toPromise()
.then(response => response.json())
.catch((error: string) => console.log(error));
}
getProjects(): Promise<Project>{
return this.getData()
.toPromise()
.then(response => response.json().projects) // <<< added .projects
.catch((error: string) => console.log(error));
}
getProject(id: string): Promise<Project>{
return this.getProjects()
.then(p => `${p}/${id}`) // no idea where `id` should come from
.then(url => {
return this.http.get(url)
.toPromise()
.then(response => response.json())
.catch((error: string) => console.log(error));
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句