首页 > 解决方案 > 如何从 javascript 对象中获取项目

问题描述

我不知道如何从这个 JSON 中获取项目。拜托,你能帮帮我吗?例如,我尝试过 JSON.stringify 或采用变量并写入 variable.items 或 variable.responseJSON.items ......但它总是不起作用。非常感谢。

JSON 响应图像

编辑:

响应文本:

{…} ​ abort:函数 abort(e) ​ always:函数 always() ​ catch:函数 catch(e) ​ done:函数 add() ​ fail:函数 add() ​ getAllResponseHeaders:函数 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 "高度": 90\n },\n "中": {\n "url": "https://i.ytimg.com/vi/WibQCYgmb8w/mqdefault.jpg",\n "宽度": 320,\n "高度": 180\n },\n "高": {\n "url": "https://i.ytimg.com/vi/WibQCYgmb8w/hqdefault.jpg",\n "宽度": 480,\n "身高": 360\n },\n "标准": {\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 "宽度": 120,\n "高度": 90\n },\n "中": {\n "url": "https://i.ytimg.com/vi/1RmUpp_iryc/mqdefault. jpg",\n "宽度": 320,\n "高度": 180\n },\n "高": {\n "url": "https://i.ytimg.com/vi/1RmUpp_iryc/ hqdefault.jpg",\n "宽度": 480,\n "高度": 360\n },\n "标准": {\n "url": "https://i.ytimg.com/vi/ 1RmUpp_iryc/sddefault.jpg",\n "宽度": 640,\n "高度": 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 "宽度": 120,\n "高度": 90\n },\n "中等": {\n "url": "https://i.ytimg.com/vi /IQsrulcu5r4/mqdefault.jpg",\n "宽度": 320,\n "高度": 180\n },\n "高": {\n "url": "https://i.ytimg.com/vi/IQsrulcu5r4/hqdefault.jpg",\n "宽度": 480,\n "高度": 360\n },\n "标准": {\n "url": "https://i. ytimg.com/vi/IQsrulcu5r4/sddefault.jpg",\n "宽度": 640,\n "高度": 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 "宽度": 120,\n "高度": 90\n },\n "中": {\n "url" :“https://i.ytimg.com/vi/F7F54YirWSs/mqdefault.jpg",\n "宽度": 320,\n "高度": 180\n },\n "高": {\n "url" : "https://i.ytimg.com/vi/F7F54YirWSs/hqdefault.jpg",\n "宽度": 480,\n "高度": 360\n },\n "标准": {\n " url": "https://i.ytimg.com/vi/F7F54YirWSs/sddefault.jpg",\n "宽度": 640,\n "高度": 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 “默认”:{\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 "标准": {\n "url": "https://i.ytimg.com/vi/rMJ-eJWXXQQ/sddefault.jpg",\n "宽度": 640,\n "高度" : 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 "宽度": 320,\n "高度": 180\n },\n "高": {\n "url ": "https://i.ytimg.com/vi/INi4Dvh2ozI/hqdefault.jpg",\n "宽度": 480,\n "高度": 360\n },\n "标准":{\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 : 函数 then(t, r, i) ​ : Object { … } videogalerie.php:257:14对象 { ... } videogalerie.php:257:14对象 { ... } videogalerie.php:257:14

标签: javascriptarraysjsonobject

解决方案


给定对象,我假设这是 jquery 的 ajax 调用的结果。

可能variable.responseJSON.items并不总是工作,因为调用是异步的,所以我有根据的猜测是你没有从正确的回调中调用它,或者 ajax 的调用可能返回错误。

例如,类似的东西:

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

console.dir(myCall.responseJSON.items);

不会工作。你应该有类似的东西:

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

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


推荐阅读