簡體   English   中英

將 json 數據解析為 typescript 類

[英]Parsing json data to typescript class

我從請求中收到 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"
   }
]

打字稿類:

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

}

如何將 json 數據解析為我的類 TestTime 的數組?

試試這個代碼

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

您應該能夠傳遞一個類型參數來取回您所期望的所有輸入內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM