gpt4 book ai didi

javascript - 如何将json与typescript接口(interface)映射?

转载 作者:行者123 更新时间:2023-12-03 01:23:22 27 4
gpt4 key购买 nike

我收到后端的回复,我正在尝试将其映射到接口(interface),但它总是抛出未定义的接口(interface)属性,如下例 Envelop 总是未定义。知道这里实现了什么错误吗?

接口(interface).ts

export interface IResult {
Envelop: Envelope;
}

export interface Envelope {
$: $;
Header: string;
Body: Body;
}
export interface $ {
"xmlns:soapenv": string;
}

export interface Body {
"trk:TrackResponse": TrackShipment;
}

main.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;
}

关于javascript - 如何将json与typescript接口(interface)映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51658103/

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