-1
我是新來的TypeScript,但我想要做的事情非常簡單,但文檔似乎沒有涵蓋對JSON的迭代。我想要做的是遍歷JSON響應數組,並將JSON對象放入每個JSON元素中。以下是我正在使用的代碼,WebStorm中的TypeScript評估程序告訴我speakerElement
是一個字符串,而不是JSON,因此我不能使用put
函數。TypeScript迭代JSON並放置JSON元素
return new Promise(resolve => {
this.http.get('http://.staging.wpengine.com/wp-json/wp/v2/cr3ativspeaker')
.map(res => res.json())
.subscribe(data => {
console.log("the speaker data is : ", data);
console.log("the speaker data is : ", JSON.stringify(data));
//get each speakers image and append it to their data
for(let speakerElement in <JSON>data) {
this.http.get('http://.staging.wpengine.com/wp-json/wp/v2/cr3ativspeaker')
.map(res => res.json())
.subscribe(response => {
speakerElement.put("media_details", response.media_details);
});
}
this.data = data;
resolve(this.data);
});
});
如果JSON是字符串形式只需使用JSON.parse將其建成一個對象 –
嘗試( speakerElement)。把(「媒體... –
DDRamone
'data'不再JSON當你進入'subscribe'的處理程序時,首先要做的就是停止JSON的事情,就像數據從服務到你的代碼的方式一樣,一旦你調用'res.json()'它就是隻是JavaScript對象和數組,因此,我們可以討論數據的實際外觀,爲此我們需要您編寫問題以添加第一個數據樣本HTTP GET調用 –