我需要帮助使用node.js访问JSON对象

时间:2016-11-06 18:58:03

标签: javascript json node.js

好的,我的代码如下所示

const request = require('request');

const bodyParser = require('body-parser')

const HOST = 'https://www.the100.io/api/v1/groups/123/gaming_sessions';

var baseRequest = request.defaults({headers: {'Authorization': 'Token token="I removed the token as its private"'}});

baseRequest(HOST,
               function (err, response, body) {

                   const context = JSON.stringify(JSON.parse(body), null, 1);
                    console.log(context);


        })

它来自一个网站&记录这样的数据:

[
{
    "id": 831354,
    "creator_id": 133819,
    "group_id": 2127,
    "name": "This is a session for Barbosa and I to get a Heroic and Challenge Mode clear. Looking for anyone to help out.",
    "start_time": "2016-11-07T18:15:00.000-08:00",
    "end_time": "2016-11-07T18:15:00.000-08:00",
    "start_date": "2016-11-06T21:00:00.000-08:00",
    "category": "Wrath of the Machine - Heroic Mode",
    "friends_only": false,
    "team_size": 6,
    "platform": "ps4",
    "gamertags": null,
    "bot_created": false,
    "time_zone": null,
    "level": null,
    "sherpa_led": false,
    "sherpa_id": null,
    "strict": false,
    "reddit_url": null,
    "reddit_title": null,
    "reddit_body": null,
    "mic_required": true,
    "group_only": true,
    "party_size": 2,
    "game_id": 1,
    "primary_users_count": 6,
    "light_level": null,
    "platform_formatted": "PS4",
    "creator_gamertag": "richmanbri713",
    "game_name": "Destiny",
    "group_name": "Model UN",
    "display_light_level": null,
    "display_mic_required": "Mic required",
    "formatted_start_date": "2016-11-06-0-0-0",
    "formatted_start_time": "Nov-11",
    "full": true,
    "has_spots_open": false,
    "confirmed_sessions": [
        {
            "id": 3559780,
            "user_id": 194005,
            "gaming_session_id": 831354,
            "reserve_spot": true,
            "user": {
                "id": 194005,
                "gamertag": "CapnBarbosa11",
                "age": 26,
                "level": 40,
                "supporter": false,
                "light_level": 369,
                "hide_age": true,
                "profanity_ok_formatted": null,
                "computed_avatar_api": "img/default-avatar.png",
                "computed_avatar_chat": "https://www.the100.io/default-avatar.png",
                "developer_badge": false
            }
        },
        {
            "id": 3559437,
            "user_id": 142553,
            "gaming_session_id": 831354,
            "reserve_spot": false,
            "user": {
                "id": 142553,
                "gamertag": "ZeroX82",
                "age": 29,
                "level": 40,
                "supporter": false,
                "light_level": 370,
                "hide_age": false,
                "profanity_ok_formatted": null,
                "computed_avatar_api": "img/default-avatar.png",
                "computed_avatar_chat": "https://www.the100.io/default-avatar.png",
                "developer_badge": false
            }
        },
        {
            "id": 3559084,
            "user_id": 103209,
            "gaming_session_id": 831354,
            "reserve_spot": false,
            "user": {
                "id": 103209,
                "gamertag": "Martensitic",
                "age": 33,
                "level": 40,
                "supporter": false,
                "light_level": 394,
                "hide_age": false,
                "profanity_ok_formatted": "Profanity OK",
                "computed_avatar_api": "https://pwntastic-avatar-production.s3.amazonaws.com/uploads/user/avatar/103209/main_a_print_above_the_rest_62_photos1_1396458281.jpg",
                "computed_avatar_chat": "https://pwntastic-avatar-production.s3.amazonaws.com/uploads/user/avatar/103209/thumb_a_print_above_the_rest_62_photos1_1396458281.jpg",
                "developer_badge": false
            }
        },
        {
            "id": 3559032,
            "user_id": 111897,
            "gaming_session_id": 831354,
            "reserve_spot": false,
            "user": {
                "id": 111897,
                "gamertag": "ini5o",
                "age": 29,
                "level": 40,
                "supporter": true,
                "light_level": 397,
                "hide_age": false,
                "profanity_ok_formatted": "Profanity OK",
                "computed_avatar_api": "https://pwntastic-avatar-production.s3.amazonaws.com/uploads/user/avatar/111897/main_500x500.jpg",
                "computed_avatar_chat": "https://pwntastic-avatar-production.s3.amazonaws.com/uploads/user/avatar/111897/thumb_500x500.jpg",
                "developer_badge": false
            }
        },
        {
            "id": 3558957,
            "user_id": 101605,
            "gaming_session_id": 831354,
            "reserve_spot": false,
            "user": {
                "id": 101605,
                "gamertag": "TheMesso",
                "age": 30,
                "level": 40,
                "supporter": false,
                "light_level": 387,
                "hide_age": false,
                "profanity_ok_formatted": "Profanity OK",
                "computed_avatar_api": "https://pwntastic-avatar-production.s3.amazonaws.com/uploads/user/avatar/101605/main_dramatic-eagle.jpg",
                "computed_avatar_chat": "https://pwntastic-avatar-production.s3.amazonaws.com/uploads/user/avatar/101605/thumb_dramatic-eagle.jpg",
                "developer_badge": false
            }
        },
        {
            "id": 3558942,
            "user_id": 133819,
            "gaming_session_id": 831354,
            "reserve_spot": false,
            "user": {
                "id": 133819,
                "gamertag": "richmanbri713",
                "age": 31,
                "level": 40,
                "supporter": false,
                "light_level": 387,
                "hide_age": false,
                "profanity_ok_formatted": "Profanity OK",
                "computed_avatar_api": "img/default-avatar.png",
                "computed_avatar_chat": "https://www.the100.io/default-avatar.png",
                "developer_badge": false
            }
        }
    ]
}
]

我已经尝试了很多不同的方式来调用数据,但我对这个&可以使用一些帮助。我希望能够提取数据的一个方面。如果可能的话,将它存储在变量中。如果你不介意解释事情,而不是像我想要的那样修理它,我将非常感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你可以替换这一行:

const context = JSON.stringify(JSON.parse(body), null, 1);

通过

const context = JSON.parse(body);

现在您将在context变量中返回对象。