【问题标题】:How to fetch specific JSON data in Angular如何在 Angular 中获取特定的 JSON 数据
【发布时间】:2021-02-13 15:32:53
【问题描述】:

我希望从下面的 JSON 中获取详细信息“联系人”。

{
"id": "65664546",
"name": "Employee 1",
"contacts": [
    {
        "id": "56546564",
        "firstName": "James",
        "lastName": "Carter",
        "email": "carter101@google.com"
    },
    {
        "id": "565465644",
        "firstName": "Simon",
        "lastName": "Deol",
        "email": "simon505@google.com"
    }
]

}

下面是我定义的接口

export interface employee {
  id: string;
  name: string;
  contacts: contact[];
}

export interface contact {
  firstName: string;
  lastName: string;
  email: string;
}

请您指教,它的 Httpget 方法和 subscribe 方法是什么样的。

【问题讨论】:

  • 多个员工,所以多个联系人数组.. 你喜欢它吗?或者你想知道如何显示它?
  • 嗨 MikeOne,我想访问单个选定员工的联系人。谢谢
  • @PratikSurani 请检查我的答案,让我知道它是否适合您。也让我寻求进一步的帮助。最美好的祝愿:-)
  • 我正在寻找 get 和 subscribe 方法来获取这些数据并显示在组件模板中
  • @PratikSurani 我已经更新了我的答案,请立即查看。 :-)

标签: angular typescript


【解决方案1】:

您必须使用 pluck rxjs 运算符从对象中获取特定的关键数据,下面是从 API 获取和订阅数据的代码。

这里是Working Example

appcomponent.ts

import { Component, VERSION } from "@angular/core";
import { pluck } from "rxjs/operators";
import { GetServiceService } from "./get-service.service";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  name = "Angular " + VERSION.major;
  constructor(private getAPI: GetServiceService) {}
  ngOnInit() {
    this.getAPI
      .getData()
      .pipe(pluck("contacts"))
      .subscribe(r => {
        console.log(r);
      });
  }
}

getService.ts

import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable, of } from "rxjs";
import { employee } from "./interface";


  export interface employee {
  id: string;
  name: string;
  contacts: contact[];
}

export interface contact {
  firstName: string;
  lastName: string;
  email: string;
}
@Injectable()
export class GetServiceService {
  constructor(private httpClient: HttpClient) {}
  getData(): Observable<employee> {
    // this.httpClient.get('url for api will be used')
    return of({
      id: "65664546",
      name: "Employee 1",
      contacts: [
        {
          id: "56546564",
          firstName: "James",
          lastName: "Carter",
          email: "carter101@google.com"
        },
        {
          id: "565465644",
          firstName: "Simon",
          lastName: "Deol",
          email: "simon505@google.com"
        }
      ]
    });
  }
}

【讨论】:

  • 您好 Suraj,非常感谢您的帮助。确实有效!
  • 谢谢你能把我的答案标记为正确吗?
  • 对不起,我忘了这样做。现在完成了,谢谢
【解决方案2】:

首先,employee接口中的Contact: Contacts[];有问题。应该是Contacts: Contact[];。要将string 转换为JSON,您可以使用 =>

之类的代码
myEmpArray:employees;

ngOnInit(){
   this.myEmpArray = JSON.parse(this.temp); //Here temp is your JSON Text.
   let mycontact=this.myEmpArray.employees[0].Contacts[0];
   console.log(mycontact);
   console.log(this.myEmpArray);
  }

您可以使用http.getsubscribe 方法,如下所示 =>

  myEmpArray:employees;
  constructor(private http: HttpClient) {
    this.http.get('https://jsonplaceholder.typicode.com/todos').subscribe((data) => {
      this.myEmpArray = data;
    });
  }

import { HttpClient } from '@angular/common/http';在你的组件文件中导入这一行并在app.module.ts中导入HttpClientModule

注意:您可以在stackblitz 中查看如何在Angular 中使用http GET。获取数据后,您可以将 JSON 解析为上面给出的对象。

【讨论】:

  • 你好链接器,谢谢你的帖子。我正在寻找 Get 方法和 Subscribe 方法。请您对此提出建议。
猜你喜欢
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多