您如何访问嵌套的json数据?

时间:2018-07-20 21:28:45

标签: javascript json

我正在尝试使用JQuery从RSS提要中访问链接元素。我通过调用api将XML RSS feed转换为JSON。 link元素是嵌套的,但是以下行不起作用。我刚刚开始学习JavaScript,所以我不确定自己在做什么错。我该如何进行这项工作?

谢谢!

var tagIndex = item.enclosure.link;

这是我的jquery代码:

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);
                }
            });
        });
    });

这是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&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"
        ]
    }
]
}

谢谢!

3 个答案:

答案 0 :(得分:0)

我将其放在fiddle中,它似乎可以工作。如果处理items数组,则可以使用map提取值:

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

并获取每个链接的数组。

答案 1 :(得分:0)

您需要在items数组中声明位置n,例如items [0] .enclosure.link

答案 2 :(得分:-2)

这应该对您有用,但前提是您遍历返回的“数据”结果。

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

如此:

for(var i = 0; i < data.length; i++) {
    var tagIndex = data[i]["items"]["enclosure"]["link"];
}
相关问题