HTTP-запрос Angular 6 HTTP не работает с файлом JSON - PullRequest
0 голосов
/ 11 октября 2018

Я пытаюсь получить информацию из публичной RSS-ленты Flickr.Я превратил его в файл JSON, который выглядит следующим образом:

[
{
    "title": "Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.",
    "pubDate": "2018-10-10 19:44:41",
    "link": "https://www.flickr.com/photos/129127425@N08/44320429435/",
    "guid": "tag:flickr.com,2005:/photo/44320429435",
    "author": "kevinmaxwell535",
    "thumbnail": "https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg",
    "description": "\n<p><a href=\"https://www.flickr.com/people/129127425@N08/\">kevinmaxwell535</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/129127425@N08/44320429435/\" title=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"><img src=\"https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg\" width=\"135\" height=\"240\" alt=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"></a></p>\n",
    "content": "\n<p><a href=\"https://www.flickr.com/people/129127425@N08/\">kevinmaxwell535</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/129127425@N08/44320429435/\" title=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"><img src=\"https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg\" width=\"135\" height=\"240\" alt=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"></a></p>\n",
    "enclosure": {
        "link": "https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_b.jpg",
        "type": "image/jpeg"
    },
    "categories": []
},
{
    "title": "IMG_20181010_171033",
    "pubDate": "2018-10-10 19:44:48",
    "link": "https://www.flickr.com/photos/137717399@N07/44320431445/",
    "guid": "tag:flickr.com,2005:/photo/44320431445",
    "author": "Hetoogappeltje",
    "thumbnail": "https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg",
    "description": "\n<p><a href=\"https://www.flickr.com/people/137717399@N07/\">Hetoogappeltje</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/137717399@N07/44320431445/\" title=\"IMG_20181010_171033\"><img src=\"https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg\" width=\"240\" height=\"180\" alt=\"IMG_20181010_171033\"></a></p>\n",
    "content": "\n<p><a href=\"https://www.flickr.com/people/137717399@N07/\">Hetoogappeltje</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/137717399@N07/44320431445/\" title=\"IMG_20181010_171033\"><img src=\"https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg\" width=\"240\" height=\"180\" alt=\"IMG_20181010_171033\"></a></p>\n",
    "enclosure": {
        "link": "https://farm2.staticflickr.com/1939/44320431445_c48588cda3_b.jpg",
        "type": "image/jpeg"
    },
    "categories": []
}
]

, и я использую метод, который вызывает его так:

private url: string = '/assets/data/flickr.json';

getImg(): Observable<Feed> {
return this.http.get(this.url);
}

, и это прекрасно работает!

проблема в том, что оригинальный json выглядит следующим образом:

{
"status": "ok",
"feed": {
    "url": "https://api.flickr.com/services/feeds/photos_public.gne",
    "title": "Uploads from everyone",
    "link": "https://www.flickr.com/photos/",
    "author": "",
    "description": "",
    "image": "https://s.yimg.com/pw/images/buddyicon.gif"
},
"items":[
{
    "title": "Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.",
    "pubDate": "2018-10-10 19:44:41",
    "link": "https://www.flickr.com/photos/129127425@N08/44320429435/",
    "guid": "tag:flickr.com,2005:/photo/44320429435",
    "author": "kevinmaxwell535",
    "thumbnail": "https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg",
    "description": "\n<p><a href=\"https://www.flickr.com/people/129127425@N08/\">kevinmaxwell535</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/129127425@N08/44320429435/\" title=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"><img src=\"https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg\" width=\"135\" height=\"240\" alt=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"></a></p>\n",
    "content": "\n<p><a href=\"https://www.flickr.com/people/129127425@N08/\">kevinmaxwell535</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/129127425@N08/44320429435/\" title=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"><img src=\"https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg\" width=\"135\" height=\"240\" alt=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"></a></p>\n",
    "enclosure": {
        "link": "https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_b.jpg",
        "type": "image/jpeg"
    },
    "categories": []
},
{
    "title": "IMG_20181010_171033",
    "pubDate": "2018-10-10 19:44:48",
    "link": "https://www.flickr.com/photos/137717399@N07/44320431445/",
    "guid": "tag:flickr.com,2005:/photo/44320431445",
    "author": "Hetoogappeltje",
    "thumbnail": "https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg",
    "description": "\n<p><a href=\"https://www.flickr.com/people/137717399@N07/\">Hetoogappeltje</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/137717399@N07/44320431445/\" title=\"IMG_20181010_171033\"><img src=\"https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg\" width=\"240\" height=\"180\" alt=\"IMG_20181010_171033\"></a></p>\n",
    "content": "\n<p><a href=\"https://www.flickr.com/people/137717399@N07/\">Hetoogappeltje</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/137717399@N07/44320431445/\" title=\"IMG_20181010_171033\"><img src=\"https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg\" width=\"240\" height=\"180\" alt=\"IMG_20181010_171033\"></a></p>\n",
    "enclosure": {
        "link": "https://farm2.staticflickr.com/1939/44320431445_c48588cda3_b.jpg",
        "type": "image/jpeg"
    },
    "categories": []
}
]
}

В этом файле запрос http get не работает.Какие-нибудь предложения относительно того, как я могу преодолеть это?

1 Ответ

0 голосов
/ 11 октября 2018

Файл JSON не является допустимым JSON.Массив после ключа «feed» не имеет ключа.

{
"status": "ok",
"feed": {
    "url": "https://api.flickr.com/services/feeds/photos_public.gne",
    "title": "Uploads from everyone",
    "link": "https://www.flickr.com/photos/",
    "author": "",
    "description": "",
    "image": "https://s.yimg.com/pw/images/buddyicon.gif"
},
[...]  <-- value is missing key
}

Я не знаю, каким должен быть этот ключ, я назвал его «feeds», но если вы укажете его:

{
"status": "ok",
"feed": {
    "url": "https://api.flickr.com/services/feeds/photos_public.gne",
    "title": "Uploads from everyone",
    "link": "https://www.flickr.com/photos/",
    "author": "",
    "description": "",
    "image": "https://s.yimg.com/pw/images/buddyicon.gif"
},
"feeds": [
{
    "title": "Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.",
    "pubDate": "2018-10-10 19:44:41",
    "link": "https://www.flickr.com/photos/129127425@N08/44320429435/",
    "guid": "tag:flickr.com,2005:/photo/44320429435",
    "author": "kevinmaxwell535",
    "thumbnail": "https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg",
    "description": "\n<p><a href=\"https://www.flickr.com/people/129127425@N08/\">kevinmaxwell535</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/129127425@N08/44320429435/\" title=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"><img src=\"https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg\" width=\"135\" height=\"240\" alt=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"></a></p>\n",
    "content": "\n<p><a href=\"https://www.flickr.com/people/129127425@N08/\">kevinmaxwell535</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/129127425@N08/44320429435/\" title=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"><img src=\"https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_m.jpg\" width=\"135\" height=\"240\" alt=\"Bakers 10th Oct 2018. Kens corner, caught on a Meps spinner. Caught several Perch too all over.\"></a></p>\n",
    "enclosure": {
        "link": "https://farm2.staticflickr.com/1922/44320429435_74fc3858b4_b.jpg",
        "type": "image/jpeg"
    },
    "categories": []
},
{
    "title": "IMG_20181010_171033",
    "pubDate": "2018-10-10 19:44:48",
    "link": "https://www.flickr.com/photos/137717399@N07/44320431445/",
    "guid": "tag:flickr.com,2005:/photo/44320431445",
    "author": "Hetoogappeltje",
    "thumbnail": "https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg",
    "description": "\n<p><a href=\"https://www.flickr.com/people/137717399@N07/\">Hetoogappeltje</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/137717399@N07/44320431445/\" title=\"IMG_20181010_171033\"><img src=\"https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg\" width=\"240\" height=\"180\" alt=\"IMG_20181010_171033\"></a></p>\n",
    "content": "\n<p><a href=\"https://www.flickr.com/people/137717399@N07/\">Hetoogappeltje</a> posted a photo:</p>\n\t\n<p><a href=\"https://www.flickr.com/photos/137717399@N07/44320431445/\" title=\"IMG_20181010_171033\"><img src=\"https://farm2.staticflickr.com/1939/44320431445_c48588cda3_m.jpg\" width=\"240\" height=\"180\" alt=\"IMG_20181010_171033\"></a></p>\n",
    "enclosure": {
        "link": "https://farm2.staticflickr.com/1939/44320431445_c48588cda3_b.jpg",
        "type": "image/jpeg"
    },
    "categories": []
}
]
}

Вы должны хотя бы уметь правильно анализировать данные.В будущем вы можете использовать линтер, такой как https://jsonlint.com/, чтобы помочь с этими типами проблем.

...