我从后端得到一个如下所示的 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(使用前将#替换为@)