将json数据解析为typescript类

时间:2017-10-16 11:24:33

标签: json typescript

我从请求中收到JSON,我需要将它转换为我的typescrypt类,您可以在下面看到

[
   {
      "id":3,
      "taskRepositoryID":null,
      "timesheetID":null,
      "timeCategoryID":null,
      "startTime":"0001-01-01T00:00:00",
      "endTime":"0001-01-01T00:00:00",
      "duration":0,
      "comment":"some comment"
   },
   {
      "id":5,
      "taskRepositoryID":null,
      "timesheetID":null,
      "timeCategoryID":null,
      "startTime":"0001-01-01T00:00:00",
      "endTime":"0001-01-01T00:00:00",
      "duration":0,
      "comment":"some comment"
   }
]

TypeScript类:

export class TestTime {
  id: number;
  taskRepositoryID: any;
  timesheetID: any;
  timeCategoryID: any;
  startTime: string;
  endTime: string;
  duration: any;
  comment: string;

}

如何将json数据解析为我的TestTime类的数组?

2 个答案:

答案 0 :(得分:0)

试试此代码

let myList:TestTime[]  = JSON.parse(myJson);

答案 1 :(得分:0)

您应该能够传递一个类型参数来获取所有类型的内容:

this._timeServcie.getTimeForRange<TestTime[]>(argumets) 
    .subscribe((data: TestTime[]) => //...