【发布时间】:2018-05-31 05:55:29
【问题描述】:
我正在尝试将 json 响应转换为角度 2 中的数组。以下是我使用的代码,
.ts 文件
response;
resp;
constructor(private http:Http) {
}
get()
{
this.http.get("http://localhost:3000/items")
.map(res=>res.json())
.subscribe(data=>this.response = JSON.stringify(data))
this.resp = JSON.parse(this.response);
}
component.html
{{response}}
<ul>
<li *ngFor="let k of resp">{{k}}</li>
</ul>
</div>
虽然 {{response}} 只是导致显示整个 json 内容,但我无法遍历在 resp 中检索到的响应,尽管使用了 JSON.parse()。
如何将响应转换为数组并访问各个属性?
【问题讨论】:
-
你能给我this.response的控制台输出吗??
标签: json typescript angular2-http