首页 > 解决方案 > 角度 6 http 获取 JSON

问题描述

我无法从某些 json 文件中获取信息。

案例1(作品):

服务:

private xmlToJson: string = 'https://rss2json.com/api.json?rss_url=';

getImg(Url: string) {
return this.http.get(this.xmlToJson + Url);
}

零件:

private url='https://api.flickr.com/services/feeds/photos_public.gne';

public images: any = [];

this.imgDataService.getImg(this.url)
  .subscribe(data => this.images = data);

HTML:

<h1>{{images.feed.title}}</h1>

案例2(不起作用):

服务:

getImg(Url: string) {
return this.http.get(Url);
}

零件:

private url = 'https://api.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1';

public images: any = [];

this.imgDataService.getImg(this.url)
  .subscribe(data => this.images = data);

HTML:

<h1>{{images.title}}</h1>

知道为什么案例 2 不起作用吗?我在这里运行了两个 JSON: https ://jsonlint.com/并且它们是有效的。

标签: jsonangularhttpget

解决方案


更改public images: any = []public images: any = {}

图像数据不是数组。


推荐阅读