gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:49:39 30 4
gpt4 key购买 nike

我从后端得到一个 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 实现此目的和 map运算符(operator)来自 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;
}

服务
this.http.get<Scheduling[]>(`${applicationUrl}/scheduling/${date}`)
.pipe(
map(schedulings => {
const modifiedSchedulings = []
for (const scheduling of schedulings) {
modifiedSchedulings.push(new Scheduling(scheduling));
}
return modifiedSchedulings;
})
);

关于json - 将日期从 JSON 反序列化为 Typescript 中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59491695/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com