如何用typescript接口映射json



我从后端得到响应,我试图将其映射到接口,但它总是抛出未定义的接口属性,就像下面的情况一样,Envelop总是未定义。你知道这里实施的错误是什么吗?

接口.ts

export interface IResult {
Envelop: Envelope;
}
export interface Envelope {
$: $;
Header: string;
Body: Body;
}
export interface $ {
"xmlns:soapenv": string;
}
export interface Body {
"trk:TrackResponse": TrackShipment;
}

主.ts

public after(data: IResult){
const result = data.Envelop.Body
const response: any = result;
return response;
}

来自后端的Json数据

"soapenv:Envelope": {
"$": {
"xmlns:soapenv": "http"
},
"soapenv:Header": "",
"soapenv:Body": {
"some test Data"
}
}

给定您的响应JSON,您的接口应该看起来像这个

export interface IResult {
"soapenv:Envelope": Envelope;
}
export interface Envelope {
$: $;
"soapenv:Header": string;
"soapenv:Body": Body;
}
export interface $ {
"xmlns:soapenv": string;
}
export interface Body {
"trk:TrackResponse": TrackShipment;
}

然后你会像一样访问

public after(data: IResult) {
const result: Body = data["soapenv:Envelope"]["soapenv:Body"];
return result;
}

最新更新