How to map JSON response to Model in Angular 4

耗尽温柔 提交于 2019-12-03 22:47:25

You need to import the interface in your component,

import { result } from '.result';

Your interface should look like,

interface RootObject {
  result: Result[];
}

interface Result {
  id: string;
  type: string;
  contactinfo: Contactinfo;
}

interface Contactinfo {
  zipcode: number;
  name: string;
}

and change the type of result as,

result : result;

and assign the result as,

this.result = data;

You can use http://www.jsontots.com/ to create the interface based on JSON

your "data" is an Object, with a property "result". result[] has a property called "contactInfo". in contactInfo you have the data you want, so

//you have no model, so NOT get<result[]>
this.http.get('url...', { headers: this.headers }).subscribe(
            data => {
                this.result= data.result[0].contactInfo;
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!