如何从JSON文件中检索这些值?

时间:2018-07-22 03:20:24

标签: javascript jquery json

我正在尝试从JSON文件访问嵌套元素。更具体地说,我从RSS提要中获取信息,并使用api转换为JSON,然后使用$ .each方法通过JQuery检索该信息。我要检索的元素是“链接”,但以下行不起作用。我只能输出标题对象。我该如何解决?

谢谢!

这是我的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>

这是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 :(得分:1)

错误@我的上一个答案;尝试以下代码:

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

答案 1 :(得分:1)

您可以尝试一下。

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

答案 2 :(得分: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;
        });
      }
    });
  })();
相关问题