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

标签 javascript jquery json node.js

我试图在此处调用此 api url 的 get:http://www.reddit.com/r/reddevils/top.json?sort=top&t=day&limit=1

我想做的就是获取永久链接数据,它是一个名为“data”的对象内部的一个元素,该对象嵌套在另一个名为“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);
});

谢谢

最佳答案

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

{
    "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/

如果你想要所有的 child ,那么你需要迭代i:

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

获取每一个。

<小时/>

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

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

关于javascript - 这里是 Node.js 的新手。如何正确从 subreddit 顶部获取 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44792901/

相关文章:

jquery - 简单的 jquery UI 自动完成页面不起作用

带有 2 个下拉菜单的 JavaScript 单位转换器

javascript - 刷新 html 以了解指令一致模型更改

javascript - 在菜单关闭后销毁 typeahead

javascript - 使用 Protractor 单击复选框

jquery - JSTree与IE(7/8) : all non-leaf nodes open after tree loading

javascript - 通过javascript代码将图像插入数据库

jquery - 从 ASP 中的表单发送的带有希伯来语文本的电子邮件的编码问题

javascript - 从 JavaScript Web 服务客户端发送 CORS POST 请求返回 http 405 错误

python - json 文件是否可以在第 n 个元素中包含不在前面元素中的键?