将 Json 映射到 Angular 对象

2024-01-09

我有以下 JSON 数据

  {
  "columns": [
    {
      "table": "black_list",
      "name": "id",
      "datatype": "uuid"
    },
    {
      "table": "black_list",
      "name": "emailid",
      "datatype": "varchar"
    },
    {
      "table": "black_list",
      "name": "membershipid",
      "datatype": "varchar"
    },
    {
      "table": "black_list",
      "name": "phonenumber",
      "datatype": "varchar"
    }
  ],
  "rows": [
    {
      "id": "59525ac0-9799-11e8-8ea0-897582b5513d",
      "emailid": "[email protected] /cdn-cgi/l/email-protection",
      "membershipid": "999999",
      "phonenumber": "1234567890"
    }
  ]
}

我的模型是

export interface BlacklistData {
    id: string;
    emailid: string;
    membershipid: string;
    phonenumber: string;   
}

最后,我使用下面的代码从 REST API 获取结果并尝试将其映射到我的对象

public GetBlackListData(): Observable<WatchlistData[]> {
        var path = "http://ec2compute/BDEServices/RestSearch?selectCls=all&fromCls=demo.black_list";
        return this.http.get(path)
            .pipe(map((result: Response) => this.BlackListData = result.json()));
    }

我只想使用 JSON 数据的行部分来映射到我的对象,但我不确定如何做到这一点。有人可以告诉我如何有选择地解析 JSON 数据并将其映射到我的对象。

我正在使用 Angular 6 并从 'rxjs/internal/Observable' 导入 { Observable };

Thanks


只需从您的组件订阅您的服务方法,如下所示:

this.yourService.GetBlackListData().subscribe(result => this.result =result.rows);

您的模型应如下所示,

    export interface Column {
        table: string;
        name: string;
        datatype: string;
    }

    export interface Row {
        id: string;
        emailid: string;
        membershipid: string;
        phonenumber: string;
    }

    export interface BlacklistData {
        columns: Column[];
        rows: Row[];
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 Json 映射到 Angular 对象 的相关文章

随机推荐