这里是node.js的新手。如何从subreddit顶部正确获取JSON?

时间:2017-06-28 02:56:14

标签: javascript jquery json node.js

我想在这里打电话给这个api url:http://www.reddit.com/r/reddevils/top.json?sort=top&t=day&limit=1

我想要做的就是获取永久链接数据,这是一个名为' data'的对象内部的元素。它嵌套在另一个名为“数据”的对象中。我需要永久链接数据,因此,我可以将其导出并在另一个文件中使用它。

我正在尝试在node.js上执行此操作,我将如何执行此操作?我试图使用http,express和ajax-request包。我觉得我走的是正确的道路,但这可能仅仅是正确的语法问题。

这就是我现在所拥有的。我只是试图将JSON对象打印到我的控制台上,看看我是否在正确的道路上:

var request = require('ajax-request');
var http = require('http');

var url = "http://www.reddit.com/r/reddevils/top.json?sort=top&t=day&limit=1";

http.get(url, function(res){
    var body = '';

    res.on('data', function(chunk){
        body += chunk;
    });

    res.on('end', function(){
        var redditResponse = JSON.parse(body);
        console.log("Got a response: ", redditResponse.picture);
    });
}).on('error', function(e){
      console.log("Got an error: ", e);
});

由于

1 个答案:

答案 0 :(得分:1)

格式化后,您的数据如下所示:

{
    "kind": "Listing",
    "data": {
        "modhash": "fop7jgfq8z07efe8b23281496fdcd30eae5038b50d6b9811d2",
        "children": [{
            "kind": "t3",
            "data": {
                "contest_mode": false,
                "subreddit_name_prefixed": "r/reddevils",
                "banned_by": null,
                "media_embed": {},
                "thumbnail_width": 140,
                "subreddit": "reddevils",
                "selftext_html": null,
                "selftext": "",
                "likes": null,
                "suggested_sort": null,
                "user_reports": [],
                "secure_media": null,
                "link_flair_text": "TIER 0",
                "id": "6jqxwx",
                "view_count": null,
                "secure_media_embed": {},
                "clicked": false,
                "report_reasons": null,
                "author": "SuperShadowJr",
                "saved": false,
                "mod_reports": [],
                "name": "t3_6jqxwx",
                "score": 792,
                "approved_by": null,
                "over_18": false,
                "domain": "instagram.com",
                "hidden": false,
                "preview": {
                    "images": [{
                        "source": {
                            "url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?s=5fe73832273b3e88324a6da17fa55fae",
                            "width": 640,
                            "height": 640
                        },
                        "resolutions": [{
                            "url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=108&s=b668298ccd3a69304ab23c5aef1b6d77",
                            "width": 108,
                            "height": 108
                        }, {
                            "url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=216&s=706a70a275e20973e8ce772f362db87f",
                            "width": 216,
                            "height": 216
                        }, {
                            "url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=320&s=42c33880cf1a0a1c14e814de5469a1a1",
                            "width": 320,
                            "height": 320
                        }, {
                            "url": "https://i.redditmedia.com/qKDgoT2EDbwYbozYQO23nDisAj6yd9I2XwdWKLw-QNQ.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=640&s=783b2011d7523d9c84450293eef9d6c0",
                            "width": 640,
                            "height": 640
                        }],
                        "variants": {},
                        "id": "TNO0lZhaqwUX-vbTYG6y0bI6hZhhgQJWTgm_OVk7b2E"
                    }],
                    "enabled": false
                },
                "thumbnail": "https://a.thumbs.redditmedia.com/8U2J6sELZ1AIGDsReUz-v10X3qyFG49TBH0XrjXAMc8.jpg",
                "subreddit_id": "t5_2rxse",
                "edited": false,
                "link_flair_css_class": "black",
                "author_flair_css_class": null,
                "gilded": 0,
                "downs": 0,
                "brand_safe": true,
                "archived": false,
                "removal_reason": null,
                "post_hint": "link",
                "can_gild": true,
                "thumbnail_height": 140,
                "hide_score": false,
                "spoiler": false,
                "permalink": "/r/reddevils/comments/6jqxwx/fabinhos_girlfriendwife_likes_ig_picture_of/",
                "num_reports": null,
                "locked": false,
                "stickied": false,
                "created": 1498574067.0,
                "url": "https://www.instagram.com/p/BV0dLySgy82/",
                "author_flair_text": null,
                "quarantine": false,
                "title": "Fabinho's girlfriend/wife likes IG picture of Fabinho in MUFC kit with the caption \"Could we see this man in the PL?\"",
                "created_utc": 1498545267.0,
                "distinguished": null,
                "media": null,
                "num_comments": 79,
                "is_self": false,
                "visited": false,
                "subreddit_type": "public",
                "is_video": false,
                "ups": 792
            }
        }],
        "after": "t3_6jqxwx",
        "before": null
    }
}

所以,你可以使用:

redditResponse.data.children[0].data.permalink

工作演示:https://jsfiddle.net/jfriend00/nkpchrk0/

如果你想要所有孩子,那么你需要在i中迭代:

redditResponse.data.children[i].data.permalink

获取每一个。

或者,要将它们全部放入自己的数组中,您可以这样做:

let permaLinks = redditResponse.data.children.map(function(child) {
    return child.data.permalink;
});