0

I'm trying to access a link element from a RSS feed with JQuery. I converted the XML RSS feed to JSON by calling the api. The link element is nested, but the following line does not work. I just started learning JavaScript so i'm not sure what i'm doing wrong. How do I make this work?

Thanks!

var tagIndex = item.enclosure.link;

Here's my jquery code:

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) {
                        output += '<div class="post-card category-medium published">';
                        //output += '<h3 class="date">' + $.format.date(item.pubDate, "dd<br>MMM") + "</h4>";
                        var tagIndex = item.enclosure.link;// Find where the media:content tag starts
                        console.log(tagIndex);
                        var urlIndex = item.description.substring(tagIndex).indexOf('url=') + tagIndex; // Find where the url attribute starts
                        var urlStart = urlIndex + 5; // Find where the actual image URL starts; 5 for the length of 'url="'
                        var urlEnd = item.description.substring(urlStart).indexOf('"') + urlStart; // Find where the URL ends
                        var url = item.description.substring(urlStart, urlEnd); // Extract just the URL
                        output += '<p class="post-meta">';
                        //output += '<span class="published">' + item.pubDate + '</span>';
                        output += '<a href="http://roundup.calpolycorporation.org" target="_blank">@roundup.calpolycorporation.org</span></a>';
                        output += '</p>';

                        output += '<h2 class="entry-title">' + item.title + '</h2>';
                        //output += '<div class="post-meta"><span>By ' + item.author + '</span></div>';
                        var yourString = item.description;
                        var maxLength = 300 // maximum number of characters to extract
                        //trim the string to the maximum length
                        var trimmedString = yourString.substr(0, maxLength);
                        //re-trim if we are in the middle of a word
                        trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))

                        output += '<div class="excerpt">'+trimmedString + '</div>';
                        output += '<a href="'+ item.link + '" class="more-link cpc-button activeghostdark small">Read More</a>';
                        output += '<a class="entry-featured-image-url" href="'+ item.link + '"><img src="' + url + '"></a>';

                        output += '</div>';
                        return k < 1;
                    });
                    $content.html(output);
                }
            });
        });
    });

This is part of the json code:

{
"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&amp;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"
        ]
    }
]
}

Thanks!

Charlie805
  • 169
  • 1
  • 11
  • I do not see the problematic statement in your ajax logic. Given what you have you can access `item.enclosure.link` in the `each` – Taplar Jul 20 '18 at 21:41

3 Answers3

0

I put it in a fiddle and it seems to work. If you deal with an array of items, you could make use of map to extract the values:

data.items.map(x=>{return x.enclosure.link}) 

and get an array of every link in return.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
0

You need to declare the position n inside items array eg items[0].enclosure.link

-2

This should work for you, but only if you iterate through the returned 'data' result.

var tagIndex = data[i]["items"]["enclosure"]["link"];

so:

for(var i = 0; i < data.length; i++) {
    var tagIndex = data[i]["items"]["enclosure"]["link"];
}
Joe
  • 9
  • 1
  • 5
  • `data[i].items` is an array, you need to loop through it. – Barmar Jul 20 '18 at 21:59
  • Thanks, but this does not work. – Charlie805 Jul 20 '18 at 23:46
  • Hmmm... not sure why it wouldn't. You need to access your json like an array, and that result is stored in data, so maybe start from there. Can you post the error your getting from the console? – Joe Jul 21 '18 at 00:20