【发布时间】:2016-05-31 20:54:51
【问题描述】:
如何使用 TypeScipt 解析复杂的 json 对象?
我有一个客户对象,他有一些发票。
这是我的模型:
export class Customer {
public id: string;
public name: string;
public invoices: CustomerInvoice[];
get invoicesCount(): number {
if (this.invoices== null) {
return 0;
}
return this.invoices.length;
}
constructor() {
}
}
export class CustomerInvoice {
public id: number;
constructor() {
}
}
在我的服务中,我有:
ngOnInit() {
if (this.id != null) {
this.dataService.getCustomer(this.id).subscribe(data => {
this.customer = data;
},
err => console.log(err));
}
}
客户数据很好(我的客户 ID、姓名等有一些值),但发票为空。
json 正确,data.Invoices.length 返回一个数字。
【问题讨论】:
标签: json typescript angular