Javascript 在TypeScript中对JSON对象数组进行迭代可以在索引处提供单个字符
我有一个从API调用接收的JSON对象数组Javascript 在TypeScript中对JSON对象数组进行迭代可以在索引处提供单个字符,javascript,json,angular,typescript,Javascript,Json,Angular,Typescript,我有一个从API调用接收的JSON对象数组 data = [{ "notificationId": null, "notificationText": "xXXX YYY", "notificationType": "typeA", "fromId": 2, "toId": 9, "timestampCreated": { "epochSecond": 1493898875, "nano":
data = [{
"notificationId": null,
"notificationText": "xXXX YYY",
"notificationType": "typeA",
"fromId": 2,
"toId": 9,
"timestampCreated": {
"epochSecond": 1493898875,
"nano": 0
}},
{
"notificationId": null,
"notificationText": "YYYYYYY,
"notificationType": "typeB",
"fromId": 3,
"toId": 9,
"timestampCreated": {
"epochSecond": 1493898903,
"nano": 0
}}
]
我希望通过迭代这个数组来获得各个通知对象
我的通知界面如下所示
interface Notification {
notificationId: number;
notificationText : string;
notificationType : string;
fromId:number;
toId:number;
timestampCreated: Date ;
}
notif: Notification[] = [];
.
.
this.notif =data;
for(var i=0;i<this.notif.length ;i++){
console.log("notification text at "+this.notif[i].notificationText);
//this gives undefined
//and it loops through all the characters of the data
// not just twice
}
接口通知{
notificationId:编号;
通知文本:字符串;
notificationType:字符串;
fromId:数字;
toId:数字;
创建时间:日期;
}
notif:通知[]=[];
.
.
this.notif=数据;
对于(var i=0;i我回答这个问题是因为我发现了问题所在。在上面的代码片段中,对象数组实际上是JS对象数组,而不是JSON对象。因此我需要先解析对象,然后才能将它们用作JSON。一个简单的JSON.parse(数据)修复了该问题。是否在角度模板中使用它?与您提到的标记主题“角度不可能”一样,发送“使用您的整个模板”code@Timothy你在尝试访问之前解析过你的JSON吗?@Jayakrishnan:谢谢,这就是问题所在。我认为JSON对象实际上是JS对象,JSON.parse解决了它。你的对象有一个布莱姆!你没有关闭第一个}!