简体   繁体   English

将HTTP响应从JSON对象转换为Typescript类

[英]Convert the http response from JSON objects to Typescript class

I have a spring boot web api running on the backend and i'm trying to cosume it from an angular 2 webApp, the HTTP response is like the following 我有一个在后台运行的spring boot web api,并且我试图从有角度的2 webApp中使用它,HTTP响应如下所示

HTTP响应

what I'm trying to do is convert the JSON object received as response to a defined Typescript class so I can use in my application 我想做的是将作为响应收到的JSON对象转换为已定义的Typescript类,以便可以在我的应用程序中使用

HTTPService.ts

Angular 2 uses subscribe method. Angular 2使用订阅方法。 Try this: 尝试这个:

this.http.get('https://www.reddit.com/r/gifs/new/.json?limit=10').map(res => res.json()).subscribe(data => {
    this.posts = data.data.children;
});

Used in Ionic 2 demo: https://www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/ 在Ionic 2演示中使用: https : //www.joshmorony.com/using-http-to-fetch-remote-data-from-a-server-in-ionic-2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM