【问题标题】:How to mapping nested json object to model in angular 11?如何将嵌套的 json 对象映射到 Angular 11 中的模型?
【发布时间】:2021-02-28 13:23:02
【问题描述】:

我想用嵌套对象填充我的 Person 类 我的 api 响应是:

{
"id": "1001614505887752423",
"type": 1,
"name": "test contact",
"createdAt": "1399/12/10 - 09:51",
"updatedAt": "1399/12/10 - 09:51",
"addresses": [
  {
    "id": "1001614505887757358",
    "personId": "1001614505887752423",
    "companyId": null,
    "cityId": 66,
    "address": "test",
    "postalCode": "485484",
    "updatedAt": "1399/12/10 - 09:51",
    "createdAt": "1399/12/10 - 09:51"
  },
  {
    "id": "100161450588776282",
    "personId": "1001614505887752423",
    "companyId": null,
    "cityId": 45,
    "address": "test",
    "postalCode": "48548",
    "updatedAt": "1399/12/10 - 09:51",
    "createdAt": "1399/12/10 - 09:51"
  }
]

}

我的 Person 类以对象数组为属性:

 export class Person {
   id: string;
   name: string;
   type: number;
   addresses: Array<Address>;

   createdAt: string;
   updatedAt: string;
  }


export class Address{
id: string;
address: string;
postalCode: string;
cityId: number;
createdAt: string;
updatedAt: string;
}

所有道具都已填充但 Address[] 未填充, 我这样称呼httpclient:

   this.http.get<Person>(url).subscribe(x => this.model = x);

【问题讨论】:

    标签: angular typescript observable httpclient


    【解决方案1】:

    我会建议两种解决方案

    1- 如果你不打算创建类方法,你可以只使用一个接口。所以你的代码变成了这样:

    export interface Person {
        id: string;
        name: string;
        type: number;
        addresses: Address[];
        createdAt: string;
        updatedAt: string;
    }
    
    export interface Address {
        id: string;
        address: string;
        postalCode: string;
        cityId: number;
        createdAt: string;
        updatedAt: string;
    }
    

    2- 如果你真的需要使用类,那么你需要初始化你的类,因为你现在正在做的不是。新代码:

    export class Person {
        id: string;
        name: string;
        type: number;
        addresses: Address[];
        createdAt: string;
        updatedAt: string;
    
        constructor(personObject: any) {
            this.id = personObject.id;
            this.name = personObject.name;
            this.type = personObject.type;
            this.createdAt = personObject.createdAt;
            this.updatedAt = personObject.updatedAt;
            this.addresses = personObject.addresses.map((a: any) => new Address(a));
        }
    }
    
    export class Address {
        id: string;
        address: string;
        postalCode: string;
        cityId: number;
        createdAt: string;
        updatedAt: string;
    
        constructor(addressObject: any) {
            this.id = addressObject.id;
            this.address = addressObject.address;
            this.postalCode = addressObject.postalCode;
            this.cityId = addressObject.cityId;
            this.createdAt = addressObject.createdAt;
            this.updatedAt = addressObject.updatedAt;
        }
    }
    

    然后你的 api 请求:

     this.http.get<Person>(url).subscribe(x => this.model = new Person(x));
    

    【讨论】:

    • 很麻烦你解决了我的问题,但是我对 FormGroup 的构造函数在这一行有一个新问题: this.formGroup = this.fb.formGroup(Person, this.model);
    • 感谢使用带问号的构造函数参数并解决了表单生成器错误
    猜你喜欢
    • 2021-07-06
    • 1970-01-01
    • 2019-09-08
    • 2021-06-20
    • 2017-04-19
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多