从服务器Angular 4解析json_callback响应

时间:2018-02-04 14:59:51

标签: json angular typescript flickr

使用Angular 4,我向此网址https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=JSON_CALLBACK&format=json&tagmode=all&tags=test

发送了一个获取请求

使用以下代码

  this.http.get('https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=JSON_CALLBACK', {
    params: { format: 'json', tagmode: 'all', tags: query }
  })
  .subscribe(data => {
    console.log(data);
  });

我得到的响应采用以下格式,包装为方法参数。

JSON_CALLBACK({
    "title": "Recent Uploads tagged test",
    "link": "https:\/\/www.flickr.com\/photos\/tags\/test\/",
    "description": "",
    "modified": "2018-02-04T14:51:42Z",
    "generator": "https:\/\/www.flickr.com",
    "items": [
   {
        "title": "Desktop2018-02-04-14_39_44.jpg",
        "link": "https:\/\/www.flickr.com\/photos\/besnaveld\/28298869359\/",
        "media": {"m":"https:\/\/farm5.staticflickr.com\/4769\/28298869359_e932760377_m.jpg"},
        "date_taken": "2018-02-04T06:51:42-08:00",
        "description": " <p><a href=\"https:\/\/www.flickr.com\/people\/besnaveld\/\">besnaveld<\/a> posted a photo:<\/p> <p><a href=\"https:\/\/www.flickr.com\/photos\/besnaveld\/28298869359\/\" title=\"Desktop2018-02-04-14_39_44.jpg\"><img src=\"https:\/\/farm5.staticflickr.com\/4769\/28298869359_e932760377_m.jpg\" width=\"240\" height=\"135\" alt=\"Desktop2018-02-04-14_39_44.jpg\" \/><\/a><\/p> ",
        "published": "2018-02-04T14:51:42Z",
        "author": "nobody@flickr.com (\"besnaveld\")",
        "author_id": "154665852@N05",
        "tags": "test booth"
   } ]
})

如何在JSON_CALLBACK函数参数中提取数据?

1 个答案:

答案 0 :(得分:0)

基于filckr文档,只需添加

nojsoncallback=1    -> {...}
jsoncallback=wooYay -> wooYay({...});

要求你做对。那么你的网址应该是:

https://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1&format=json&tagmode=all&tags=test