将日期从 JSON 反序列化为 Typescript 中的日期

2023-11-22

我从后端得到一个如下所示的 JSON:

[{
    "schedulingId": "7d98a02b-e14f-43e4-a8c9-6763ba6a5e76",
    "schedulingDateTime": "2019-12-28T14:00:00",
    "registrationDateTime": "2019-12-24T16:47:34",
    "doctorViewModel": {
        "doctorId": "a49d9534-65a6-4730-ac45-4dc2f91165e0",
        "doctorName": "Ana"
    },
    "personViewModel": {
        "personId": "3607c475-e287-4e83-85e6-a46f4a0116d6",
        "personName": "João",
        "birthDate": "1970-09-18T00:00:00"
    },
    "consultaViewModel": null
}]

我需要将该 JSON 反序列化到我的调度中,将日期从 JSON 解析为“dd/MM/yyyy hh:MM:ss”格式:

export interface Scheduling {
   schedulingId : string;
   schedulingDateTime : Date;
   registrationDateTime : Date;
   person : Person;
   doctor : Doctor;
   consulta? : Consulta;
}

export interface Person {
   personId : string;
   personName : string;
   birthDate : Date;
}

export interface Doctor {
    doctorId : string;
    doctorName : string;
}

export interface Consulta {
    consultaId : string;
    consultaDateTime : Date;
}

但我不确定在调用 get 方法后如何做到这一点:

this.httpClient.get<Scheduling>(`${applicationUrl}/scheduling/${date}`);

有两种方法可以实现这一点,使用直接映射和使用构造函数,

您可以使用以下方法实现此目的pipe and map运算符来自rxjs/operators

1. 直接映射

import { map } from "rxjs/operators";

this.httpClient.get<Scheduling>(`${applicationUrl}/scheduling/${date}`)
   .pipe(
        map(scheduling => {
          scheduling.schedulingDateTime = new Date(scheduling.schedulingDateTime);
          scheduling.registrationDateTime = new Date(scheduling.registrationDateTime);
          scheduling.personViewModel.birthDate = new Date(scheduling.personViewModel.birthDate);
          return scheduling;
        })
    );

2. 使用构造函数

这里将把每个接收到的 json 传递给类的构造函数,并解析构造函数中的日期。

export class Person {
  personId: string;
  personName: string;
  birthDate: Date;

  constructor(init: Person) {
    this.personId = init.personId;
    this.personName = init.personName;
    this.birthDate = parseDate(init.birthDate);
  }
}

export class Consulta {
  consultaId: string;
  consultaDateTime: Date;

  constructor(init: Consulta) {
    this.consultaId = init.consultaId;
    this.consultaDateTime = parseDate(init.consultaDateTime);
  }
}

export class Doctor {
  doctorId: string;
  doctorName: string;

  constructor(init: Doctor) {
    this.doctorId = init.doctorId;
    this.doctorName = init.doctorName;
  }
}


export class Scheduling {
  schedulingId: string;
  schedulingDateTime: Date;
  registrationDateTime: Date;
  person: Person;
  doctor: Doctor;
  consulta?: Consulta;

  constructor(init: Scheduling) {

    this.schedulingId = init.schedulingId;

    this.schedulingDateTime = parseDate(init.schedulingDateTime);
    this.registrationDateTime = parseDate(init.registrationDateTime);

    this.person = init.person !== undefined ? new Person(init.person) : undefined;
    this.consulta = init.consulta !== undefined ? new Consulta(init.consulta) : undefined;
    this.doctor = init.doctor !== undefined ? new Doctor(init.doctor) : undefined;
  }
}



export function parseDate(str: string | Date): Date {
  if (str !== undefined && str !== null) {
    return new Date(str);
  }
  return undefined;
}

Service

this.http.get<Scheduling[]>(`${applicationUrl}/scheduling/${date}`)
      .pipe(
        map(schedulings => {
          const modifiedSchedulings = []
          for (const scheduling of schedulings) {
            modifiedSchedulings.push(new Scheduling(scheduling));
          }
          return modifiedSchedulings;
        })
      );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将日期从 JSON 反序列化为 Typescript 中的日期 的相关文章

随机推荐

  • 无法通过 Composer 安装包:缺少 PHP 扩展 zip

    我尝试在 Ubuntu PC 上使用 Composer 安装 laravel 安装程序 但在安装过程中出现此错误 您的要求无法解决为一组可安装的软件包 Problem 1 laravel installer v1 4 1 requires
  • 如何使用 C 在 Unix 上复制文件?

    我正在寻找 Win32 的 Unix 等效项CopyFile 我不想通过编写自己的版本来重新发明轮子 无需调用不可移植的 API 例如sendfile 或向外部公用事业提供资金 70 年代有效的方法现在仍然有效 include
  • 如何在 MongoDB 聚合中使用 $lookup 作为 INNER JOIN?

    我用过 lookup在我的聚合查询中 但正如我所见 它的工作原理是LEFT OUTER JOIN 我想获取完全匹配的文档 INNER JOIN lookup 有什么办法可以完成吗 这是我的inventory收藏 1 id 1 sku abc
  • 我想在运行时获取变量的类型

    我想在运行时获取变量的类型 我该怎么做呢 因此 严格来说 变量的类型 始终存在 并且可以作为类型参数传递 例如 val x 5 def f T v T v f x T is Int the type of x 但取决于你想要什么do
  • 使用 python nltk 查找两个网页之间的相似性?

    我想知道两个网页是否相似 有人可以建议带有 wordnet 相似性函数的 python nltk 是否有帮助以及如何帮助 在这种情况下使用的最佳相似度函数是什么 The spotsigs提到的论文乔伊塞斯坎解决了内容重复检测问题 其中包含大
  • Nuxt:如何防止nuxt-link转到另一个页面?

    我需要停下来nuxt link转到另一个页面 这是我的代码
  • Flask:当用户在执行长进程时关闭浏览器会发生什么?

    情况如下 我有一个应用程序 它使用 Angularjs 作为前端 使用 Flask 作为后端 我有一条看起来像这样的路线 app route api route1 methods POST def route1 result some pa
  • GUI/TUI Linux 库

    是否有任何 UI 库可以从同一源构建文本用户界面 ncurses 和图形用户界面 GTK QT 我知道 debconf 可以与各种前端一起使用 我想构建类似但可编程的东西 该库支持 YaST 独立性 使用一个代码库执行 ncurses gt
  • Rails 中列名的别名

    在我的数据库中有 删除 或 监听控制 等列名 这些无法更改 因此我想为这些名称起别名以避免我的应用程序出现问题 I found 下面的代码但它已经过时了 2005 年 8 月 5 日 并且不适用于 Rails 3 module Legacy
  • 奇数时选择最后一个子项,偶数时选择最后 2 个子项

    我所处的情况是显示的元素数量是可变的 我需要一个奇怪的解决方案 但我无法实现 我什至怀疑它是否只能通过 css 来实现 如果元素数量是奇数 我需要选择最后一个子元素 如果元素数量是偶数 我需要选择最后 2 个子元素 我一直在尝试nth la
  • Yq:检索对象键名称

    我有一个 YAML 文件 在我的例子中是 docker compose 文件 如下所示 networks foo some opts covfefe bar some opts such wow services apache image
  • 空 HTML href 会导致 IE 中列出目录

    我有一个带有单独 HTML 文件的网站 实际上是 shtml 但这对于这个问题来说并不重要 这些 shtml 文件包含一张图片以及一个前进和后退按钮 因此我可以切换回预览 shtml 文件或浏览到下一个文件 就像在画廊中一样 所有这些 sh
  • 私有字段的 Scala 名称修改和 JavaFX FXML 注入

    下面的例子和解释很长 所以这是我的问题的要点 当使用坚持执行字段注入 在真正应该保持私有的字段上 的框架时 如何处理scalac对私有字段的名称修改 我正在 Scala 中使用 ScalaFX JavaFX 和 FXML 编写一个应用程序
  • Bootstrap 4 导航栏垂直显示而不是水平显示

    我已经按照教程中的方式构建了一个导航栏 但不知何故 我的导航栏在应该水平显示时却垂直显示 关于如何解决这个问题有什么想法吗 提前致谢
  • CakePHP SwiftMailer SMTP TLS OpenSSL 错误 SSL3_GET_RECORD:版本号错误

    我正在尝试使用我在这里找到的 CakePHP SwiftMailer 组件发送电子邮件 http bakery cakephp org articles sky l3ppard 2009 11 07 updated swiftmailer
  • MongoDB 自定义序列化器实现

    我是 MongoDB 新手 正在尝试让 C 驱动程序序列化 F 类 我让它使用可变 F 字段和无参数构造函数与类自动映射器一起工作 但实际上我需要保留不变性 因此我开始考虑实现 IBsonSerializer 来执行自定义序列化 我还没有找
  • 通过 Jenkins API 获取子项目构建

    我配置了一个 Jenkins 项目 我在这里称之为 SuperJob 来简单地按顺序调用几个不同的其他 jenkins 项目 我希望能够通过 Jenkins API 找出该 SuperJob 特定构建号的所有子项目的结果 查看发布的代码HE
  • Vim:突出显示 incsearch 中的所有匹配项

    我正在使用incsearch and hlsearch选项 按 Enter 键后 所有匹配项都会突出显示 但只有我键入时的第一个匹配项 我想在输入时突出显示所有匹配项 我怎样才能得到这种行为 您可以使用incsearch vim 插件 In
  • 修改 Pods 文件后 Cocoa pod 出现问题

    当我更改 Pods 文件时 在命令 pod install 后收到此错误 Users mac Documents Projects Test Podfile 1 syntax error unexpected tINTEGER expect
  • 将日期从 JSON 反序列化为 Typescript 中的日期

    我从后端得到一个如下所示的 JSON schedulingId 7d98a02b e14f 43e4 a8c9 6763ba6a5e76 schedulingDateTime 2019 12 28T14 00 00 registration