-1

I cant figure out, how to get items from this JSON. Please, can you help me? I have tried for example JSON.stringify or taken variable and written variable.items or variable.responseJSON.items...but it always doesnt work. Thank you very much.

JSON response image

EDIT:

responseText:

{…} ​ abort: function abort(e) ​ always: function always() ​ catch: function catch(e) ​ done: function add() ​ fail: function add() ​ getAllResponseHeaders: function getAllResponseHeaders() ​ getResponseHeader: function getResponseHeader(e) ​ overrideMimeType: function overrideMimeType(e) ​ pipe: function pipe() ​ progress: function add() ​ promise: function promise(e) ​ readyState: 4 ​ responseJSON: Object { items: (6) […] } ​ responseText: "{\n "items": [\n {\n "id": "WibQCYgmb8w",\n "snippet": {\n "title": "27 8 Kněhyně štafeta dvojic starší žáci 1 pokus",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/WibQCYgmb8w/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/WibQCYgmb8w/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/WibQCYgmb8w/hqdefault.jpg",\n "width": 480,\n "height": 360\n },\n "standard": {\n "url": "https://i.ytimg.com/vi/WibQCYgmb8w/sddefault.jpg",\n "width": 640,\n "height": 480\n }\n }\n }\n },\n {\n "id": "1RmUpp_iryc",\n "snippet": {\n "title": "27 8 Kněhyně štafeta dvojic starší žáci 2 pokus",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/1RmUpp_iryc/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/1RmUpp_iryc/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/1RmUpp_iryc/hqdefault.jpg",\n "width": 480,\n "height": 360\n },\n "standard": {\n "url": "https://i.ytimg.com/vi/1RmUpp_iryc/sddefault.jpg",\n "width": 640,\n "height": 480\n }\n }\n }\n },\n {\n "id": "IQsrulcu5r4",\n "snippet": {\n "title": "27 8 Kněhyně štafeta dvojic mladší žáci 1 pokus",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/IQsrulcu5r4/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/IQsrulcu5r4/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/IQsrulcu5r4/hqdefault.jpg",\n "width": 480,\n "height": 360\n },\n "standard": {\n "url": "https://i.ytimg.com/vi/IQsrulcu5r4/sddefault.jpg",\n "width": 640,\n "height": 480\n }\n }\n }\n },\n {\n "id": "F7F54YirWSs",\n "snippet": {\n "title": "27 8 Kněhyně štafeta dvojic mladší žáci 2 pokus",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/F7F54YirWSs/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/F7F54YirWSs/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/F7F54YirWSs/hqdefault.jpg",\n "width": 480,\n "height": 360\n },\n "standard": {\n "url": "https://i.ytimg.com/vi/F7F54YirWSs/sddefault.jpg",\n "width": 640,\n "height": 480\n }\n }\n }\n },\n {\n "id": "rMJ-eJWXXQQ",\n "snippet": {\n "title": "27 8 Kněhyně požární útok mladší žáci 1 pokus",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/rMJ-eJWXXQQ/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/rMJ-eJWXXQQ/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/rMJ-eJWXXQQ/hqdefault.jpg",\n "width": 480,\n "height": 360\n },\n "standard": {\n "url": "https://i.ytimg.com/vi/rMJ-eJWXXQQ/sddefault.jpg",\n "width": 640,\n "height": 480\n }\n }\n }\n },\n {\n "id": "INi4Dvh2ozI",\n "snippet": {\n "title": "27 8 Kněhyně požární útok mladší žáci 2 pokus",\n "thumbnails": {\n "default": {\n "url": "https://i.ytimg.com/vi/INi4Dvh2ozI/default.jpg",\n "width": 120,\n "height": 90\n },\n "medium": {\n "url": "https://i.ytimg.com/vi/INi4Dvh2ozI/mqdefault.jpg",\n "width": 320,\n "height": 180\n },\n "high": {\n "url": "https://i.ytimg.com/vi/INi4Dvh2ozI/hqdefault.jpg",\n "width": 480,\n "height": 360\n },\n "standard": {\n "url": "https://i.ytimg.com/vi/INi4Dvh2ozI/sddefault.jpg",\n "width": 640,\n "height": 480\n }\n }\n }\n }\n ]\n}\n" ​ setRequestHeader: function setRequestHeader(e, t) ​ state: function state() ​ status: 200 ​ statusCode: function statusCode(e) ​ statusText: "OK" ​ then: function then(t, r, i) ​ : Object { … } videogalerie.php:257:14

  • 1
    Why `JSON.stringify()` when it's already JSON? – Andreas Jul 12 '20 at 11:51
  • 1
    Try JSON.parse(responseText). Please share the complete responseText so that we can let you know, how exactly it will work. – KushalSeth Jul 12 '20 at 11:53
  • JSON.parse says this: Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data – Tomáš Kretek Jul 12 '20 at 11:54
  • Please share the string of responseText – KushalSeth Jul 12 '20 at 11:55
  • You are getting Uncaught SyntaxError error because your value is starting with double quote and internally also it has double quotes. take the value of responseText, and it should start with single quote. replace double quote with single quote. then do try.parse(). – KushalSeth Jul 12 '20 at 12:08

1 Answers1

0

Given the object, I assume this is a result of a jquery's ajax call.

Probably variable.responseJSON.items doesn't work always because the call is async, so my educated guess is that you're not call it from the proper callback, or maybe the ajax's call returns an error.

For example, something like that:

let mycall = $.ajax({
  url: 'mydata.json',
  dataType: 'json',
);

console.dir(myCall.responseJSON.items);

Won't work. You should have something like:

let mycall = $.ajax({
  url: 'mydata.json',
  dataType: 'json',
).done(function() {
  console.dir(myCall.responseJSON.items);
});

See: https://api.jquery.com/jquery.ajax/

ZER0
  • 24,846
  • 5
  • 51
  • 54
  • Oh thanks, this was one mistake. But it still dont work properly. U use this now: videosInfo = $.get("https://www.googleapis.com/youtube/v3/videos?key=API_KEY&part=snippet&fields=items(id,snippet(title, thumbnails))&id=" + videoIDs).done(function(){console.log(videosInfo.items); console.log(videosInfo); and videosInfo.items says "undefined". – Tomáš Kretek Jul 12 '20 at 12:10
  • Forgotten to write there responseJSON. Now it works. Thank you very much! – Tomáš Kretek Jul 12 '20 at 12:13