如何在json中获得RESTAPI的结果

如何在json中获得RESTAPI的结果,json,angular,rest,ionic3,Json,Angular,Rest,Ionic3,这是我第一次使用离子3和角5 我想对RESTAPI做一个简单的调用,并在列表中显示结果 我发现有必要创建一个提供者,负责进行调用 在谷歌导航时,我看到了一个例子,我的提供商是: import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; //import 'rxjs/add/operator/map' /* Generated class for the Peop

这是我第一次使用离子3和角5

我想对RESTAPI做一个简单的调用,并在列表中显示结果

我发现有必要创建一个提供者,负责进行调用

在谷歌导航时,我看到了一个例子,我的提供商是:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
//import 'rxjs/add/operator/map'
/*
  Generated class for the PeopleServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class PeopleServiceProvider {

  dati : string
  constructor(public http: HttpClient) {
    console.log('Hello PeopleServiceProvider Provider');
  }

  load() {
  if (this.dati) {
    // already loaded data
    return Promise.resolve(this.dati);
  }

  // don't have the data yet
  return new Promise(resolve => {
    // We're using Angular HTTP provider to request the data,
    // then on the response, it'll map the JSON data to a parsed JS object.
    // Next, we process the data and resolve the promise with the new data.

    this.http.get('https://randomuser.me/api/?results=10')
      //.map(res => res.json())
      .subscribe(data => {
        // we've got back the raw data, now generate the core schedule data
        // and save the data for later reference
        console.log(data);
        this.dati = data["results"];
        resolve(this.dati);
      });
  });
}
}
然后我有了我的模板,一切都很好。我的问题更像是一个疑问

当我从api获得响应时,要获得结果,我必须执行以下操作:

data["results"]
我认为我能够做到:

data.results

我错过什么了吗?谢谢

新的HttpClient允许您使用类型脚本类型检查

因此,您应该创建一个描述您的退货的界面:

interface MyReturnType {
  results: any // <-- i dont know your model
}

...

this.http.get<MyReturnType>('https://randomuser.me/api/?results=10')
.subscribe(data => {
    this.dati = data.results;
});

你从哪里得到这个例子的?请看这里,我解决了创建接口的问题,但这是唯一的方法吗?对不起,这是第一次,我想澄清我的疑虑。谢谢如果您认为data是一个对象,那么您应该能够通过执行data[results]或data来检索结果。结果,我们可以看一下您的服务器响应吗?链接文档有您问题的答案…@YounesM我在web上使用一个假的api:谢谢!我已经试过了,效果很好!!!正如我在自己回答的评论中所说,这是实现我想要的唯一方法?如果不添加类型脚本,请将返回视为angular HttpClient设置的对象默认类型定义,因此必须使用[xxx]符号