将 Json 映射到有角度的对象

编程入门 行业动态 更新时间:2024-10-17 19:25:39
本文介绍了将 Json 映射到有角度的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有以下 JSON 数据

I have the below JSON data

  {
  "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": "bid@email",
      "membershipid": "999999",
      "phonenumber": "1234567890"
    }
  ]
}

我的模型是

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

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

and finally I am using the below code to fetch the result from the REST API and trying to map it to my object

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 数据并将其映射到我的对象.

I only want to use the rows part of the JSON data to map to my object and I am not sure how I can do that. Can someone please tell me how to selectively parse JSON data and map it to my object.

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

I am using Angular 6 and import { Observable } from 'rxjs/internal/Observable';

谢谢

推荐答案

只需从您的组件订阅您的服务方法,

Just subscribe to your service method from your component as,

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[];
    }

这篇关于将 Json 映射到有角度的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 16:40:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:角度   对象   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!