打字稿如何将类分配给JSON对象数组
假设我有JSON对象的数组,如何将类或分配给它?
console.log('jsonBody ' + jsonBody);
//print jsonBody [object Object],[object Object]
console.log('jsonBody ' + JSON.stringify(jsonBody));
//print jsonBody [{"created_at":"2016-02-04","updated_at":"2016-02-04 00:45:56.000"}, {"created_at":"2016-02-04","updated_at":"2016-02-04 00:45:56.000"}]
//typescript class
export class Report {
created_at!: Date;
updated_at!: Date;
}
Let say if I have a array of JSON object, how to cast or assign the class (Report class) to it?
console.log('jsonBody ' + jsonBody);
//print jsonBody [object Object],[object Object]
console.log('jsonBody ' + JSON.stringify(jsonBody));
//print jsonBody [{"created_at":"2016-02-04","updated_at":"2016-02-04 00:45:56.000"}, {"created_at":"2016-02-04","updated_at":"2016-02-04 00:45:56.000"}]
//typescript class
export class Report {
created_at!: Date;
updated_at!: Date;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个接受JSON类型对象的构造函数:
Create a constructor that accepts an object of the json type: