我觉得我有一个非常简单的问题,但我只是没有得到答案...... 我解析响应的.text()从我的使用JSON服务器。解析为JSON。 实际上我可以访问JSON的值:this.temp[0].name
。 现在我只想遍历该JSON并将值推入字符串数组。但我怎么能做到这一点?转换JSON为一个字符串数组中的打字稿
当我尝试使用for...of
编译器想要一个字符串或数组而不是一个JSON。当我尝试使用for...in
时,它实际上不会进入循环。
下面是一些代码,我可以罚款访问:
this._pS.getAllProjects()
.subscribe(data => this.temp = JSON.parse(data.text()),
err => console.log(err),
() => console.log("hello " + this.temp[0].name + this.temp[1].name));
当我刚刚把它打印出来后.stringify():
this._pS.getAllProjects()
.subscribe(data => this.temp = JSON.stringify(data),
err => console.log(err),
() => console.log("hello " + this.temp));
我在浏览器控制台中看到这一点:
{"_body":"[{\"name\":\"Projekt A\"},{\"name\":\"Projekt XYZ\"}]",
"status":200,
"ok":true,
"statusText":"Ok",
"headers":{"Content-Type":["application/json; charset=utf-8"]},
"type":2,
"url":"http://127.0.0.1:3000/api/getAllProjectNames"}
tl; dr:我怎样才能得到身体的文本部分的值到一个字符串数组?
预先感谢