首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问嵌套的json数据?

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

Stack Overflow用户
提问于 2018-07-21 05:28:45
回答 3查看 89关注 0票数 0

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

谢谢!

代码语言:javascript
复制
var tagIndex = item.enclosure.link;

下面是我的jquery代码:

代码语言:javascript
复制
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代码的一部分:

代码语言:javascript
复制
{
"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"
        ]
    }
]
}

谢谢!

EN

回答 3

Stack Overflow用户

发布于 2018-07-21 05:32:35

我把它放在一个fiddle中,它似乎起作用了。如果要处理items数组,则可以使用map来提取值:

代码语言:javascript
复制
data.items.map(x=>{return x.enclosure.link}) 

并获取返回的每个链接的数组。

票数 0
EN

Stack Overflow用户

发布于 2018-07-21 05:36:32

您需要在项目数组中声明位置n,例如items.enclosure.link

票数 0
EN

Stack Overflow用户

发布于 2018-07-21 05:35:16

这对你来说应该是有效的,但只有当你迭代返回的'data‘结果时。

代码语言:javascript
复制
var tagIndex = data[i]["items"]["enclosure"]["link"];

所以:

代码语言:javascript
复制
for(var i = 0; i < data.length; i++) {
    var tagIndex = data[i]["items"]["enclosure"]["link"];
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51450595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档