![](/img/trans.png)
[英]angular 2 how to assign an array of JSON response using Observable and map in typescript interface and retrieve in html using *ngFor?
[英]how to map json with typescript 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.