json - 角度 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/并且它们是有效的。
解决方案
更改public images: any = []
为public images: any = {}
图像数据不是数组。
推荐阅读
- jquery - 如何覆盖 jQuery 函数
- python-3.x - 从阿拉伯语字符串中删除英文单词
- plpgsql - 为 plpgsql 函数返回复合类型时的意外行为
- apache-kafka - 如何为数据库事务实现 kafka 手动偏移提交
- html - 使用 CSS 居中内联块
- laravel - Laravel:为什么每条路线都执行两次?
- jquery - allowadditions 下拉菜单的语义 UI 设置值
- python - 如何在 Google Cloud Infrastructure 中通过电子邮件发送脚本结果(关于 Pub/Sub 和电子邮件及其他选项的设计查询)
- javascript - 从 Object.defineProperties() 定义的函数调用中获取“this”
- html - Bootstrap Multiselect - 冲突的CSS