Search code examples
jsonangulartypescriptflickr

Parsing json_callback response from server Angular 4


Using Angular 4, I sent a get request to this url https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=JSON_CALLBACK&format=json&tagmode=all&tags=test

with following code,

  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);
  });

The response I get is in the following format, wrapped as a method argument.

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": "[email protected] (\"besnaveld\")",
        "author_id": "154665852@N05",
        "tags": "test booth"
   } ]
})

How can I extract the data inside this JSON_CALLBACK function argument?


Solution

  • based on filckr document simply add

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

    to your request to get it right . then your url should be :

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