[英]How do I retrieve these value from a JSON file?
I'm trying to access a nested element from a JSON file. 我正在尝试从JSON文件访问嵌套元素。 To be more specific i'm getting information from a RSS feed and converting to JSON with an api and retrieving that information with JQuery using the $.each method.
更具体地说,我从RSS提要中获取信息,并使用api转换为JSON,然后使用$ .each方法通过JQuery检索该信息。 The element that i'm trying to retrieve is "link", but the following line does not work.
我要检索的元素是“链接”,但以下行不起作用。 I'm just only able to output the title object.
我只能输出标题对象。 How do i fix this?
我该如何解决?
Thanks! 谢谢!
Here's my JQuery code: 这是我的JQuery代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Js Test</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> </head> <body> <script> jQuery(document).ready(function ($) { $(function () { var $content = $('#roundupContent'); var data = { rss_url: 'http://roundup.calpolycorporation.org/~api/papers/b686f300-0de4-458f-9b51-07756c12d705/rss' }; $.get('https://api.rss2json.com/v1/api.json', data, function (response) { if (response.status == 'ok') { var output = ''; $.each(response.items, function (k, item) { //I can output the title var title = item.title; console.log(title); //But i cant output the image link var tagIndex = item.enclosure.link; console.log(tagIndex); return k < 1; }); } }); }); }); </script> </body> </html>
This is part of the JSON code: 这是JSON代码的一部分:
{
"items": [
{
"title": "Mustangs in Pros: Marinconz, Meyer Hitting Well in Start to Pro Career",
"pubDate": "2018-07-11 04:00:00",
"link": "https://www.gopoly.com/sports/bsb/2017-18/releases/20180711m0l4q1",
"guid": "b385bac9-6997-4bd8-b6d6-71c4ab011fdf",
"author": "",
"thumbnail": "",
"description": "<p><strong>gopoly.com</strong> - SAN LUIS OBISPO, Calif. — The top hitters for former Cal Poly position players in professional baseball are Mitch Haniger of the Seattle Mariners, Kyle Marinconz of the Auburn Doubledays, Nick Meyer …</p>",
"content": "<p><strong>gopoly.com</strong> - SAN LUIS OBISPO, Calif. — The top hitters for former Cal Poly position players in professional baseball are Mitch Haniger of the Seattle Mariners, Kyle Marinconz of the Auburn Doubledays, Nick Meyer …</p>",
"enclosure": {
"link": "http://www.gopoly.com/sports/bsb/2017-18/Marinconz-MeyerMinors.jpg?max_width=600&max_height=600"
},
"categories": [
"Sports"
]
},
{
"title": "Cal Poly's digital transformation hub lets students take technology to government",
"pubDate": null,
"link": "https://edscoop.com/california-polytechnic-state-university-digital-transformation-hub-lets-students-take-technology-to-government",
"guid": "86cec9b0-2be1-4438-90fa-ff1a4564655f",
"author": "",
"thumbnail": "",
"description": "<p><strong>edscoop.com</strong> - Students at California Polytechnic State University (Cal Poly) don’t have to go far to get real-world problem-solving experience with the latest technology. Since last October, Cal Poly students and …</p>",
"content": "<p><strong>edscoop.com</strong> - Students at California Polytechnic State University (Cal Poly) don’t have to go far to get real-world problem-solving experience with the latest technology. Since last October, Cal Poly students and …</p>",
"enclosure": {
"link": "https://s3.amazonaws.com/edscoop-media/uploads/dxhub.jpg?mtime=20180712160104"
},
"categories": [
"Technology"
]
}
]
}
Error @ my prev answer; 错误@我的上一个答案; try this code:
试试这个代码:
var $content = $('#roundupContent');
var data = {
rss_url: 'http://roundup.calpolycorporation.org/~api/papers/b686f300-0de4-458f-9b51-07756c12d705/rss'
};
$.get('https://api.rss2json.com/v1/api.json', data, function (response) {
console.log(response);
if (response.status == 'ok') {
var output = '';
$.each(response.items, function (k, item) {
//I can output the title
var title = item.title;
var enclosure = item.enclosure.link;
console.log(title);
console.log(enclosure);
return k < 1;
});
}
});
You can try this. 你可以试试看
jQuery(document).ready(function ($) {
$(function () {
var $content = $('#roundupContent');
var data = {
rss_url: 'http://roundup.calpolycorporation.org/~api/papers/b686f300-0de4-458f-9b51-07756c12d705/rss'
};
$.get('https://api.rss2json.com/v1/api.json', data, function (response) {
if (response.status == 'ok') {
var output = '';
$.each(response.items, function (k, item) {
//I can output the title
var title = item.title;
var tagIndex;
// If you want to store link in array
var tagIndexes = [];
$.each(item.enclosure, function (index, link) {
tagIndex = link;
tagIndexes.push(link)
});
console.log(tagIndex);
console.log(tagIndexes);
return k < 1;
});
}
});
});
});
(
function () {
var $content = $('#roundupContent');
var data = {
rss_url: 'http://roundup.calpolycorporation.org/~api/papers/b686f300-0de4-458f-9b51-07756c12d705/rss'
};
$.getJSON('https://api.rss2json.com/v1/api.json', data, function (response) {
if (response.status == 'ok') {
var output = '';
$.each(response.items, function (k, item) {
console.dir(item);
//I can output the title
var title = item.title;
console.log(title);
//But i cant output the image link
var tagIndex = item.enclosure.link;
console.log(tagIndex);
return k < 1;
});
}
});
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.