【问题标题】:Parsing JSON in nodejs with api.call使用 api.call 在 nodejs 中解析 JSON
【发布时间】:2015-08-16 02:59:21
【问题描述】:

我正在尝试解析来自我正在使用的 API 的 JSON 响应。我没有直接访问该文件的权限,我可以对响应进行字符串化,但我根本无法解析 JSON,理想情况下,我希望变量中的“display_name”值。

这是我所拥有的:

var api = require('twitch-irc-api');

api.call({
    channel: null,
    method: 'GET',
    path: '/channels/greatbritishbg/follows',
    options: {
        limit: 1,
        offset: 0
    }
}, function(err, statusCode, response) {
    if (err) {
        console.log(err);
        return;
    }
    console.log('Status code: ' + statusCode);
    console.log('Response from Twitch API:');
    console.log(JSON.stringify(response));
    console.log(JSON.parse(response);
});

字符串化响应:

来自 Twitch API 的响应: {"关注":[{"created_at":"2015-06-02T06:11:10Z","_links":{"self":"https://api.t witch.tv/kraken/users/feddecampo/follows/channels/greatbritishbg"},"通知 s":false,"user":{"_id":50947595,"name":"feddecampo","created_at":"2013-11-01T17: 22:49Z","updated_at":"2015-05-30T19:50:21Z","_links":{"self":"https://api.twitch .tv/kraken/users/feddecampo"},"display_name":"FeddeCampo","logo":"http://static- cdn.jtvnw.net/jtv_user_pictures/feddecampo-profile_image-d4f7de23b28e0a86-300x30 0.jpeg","bio":"Playstation MVP Sony and PS fan","type":"user"}}],"_total":2315, "_links":{"self":"https://api.twitch.tv/kraken/channels/greatbritishbg/follows?d irection=DESC&limit=1&offset=0","next":"https://api.twitch.tv/kraken/channels/gr eatbritishbg/follows?direction=DESC&limit=1&offset=1"}}

【问题讨论】:

  • 试试console.log(response.follows[0].user.display_name);
  • "...但我根本无法解析 JSON" - 使用 JSON.parse 时会发生什么?您能否至少提供一个返回数据结构的示例,以防止人们不得不查看 API 文档...
  • @thefourtheye 你太棒了。谢谢。

标签: json node.js


【解决方案1】:

response 已被“解析”

如果你格式化输出,它会变得更容易阅读:

{  
   "follows":[  
      {  
         "created_at":"2015-06-02T06:11:10Z",
         "_links":{  
            "self":"https://api.t witch.tv/kraken/users/feddecampo/follows/channels/greatbritishbg"
         },
         "notification s":false,
         "user":{  
            "_id":50947595,
            "name":"feddecampo",
            "created_at":"2013-11-01T17: 22:49Z",
            "updated_at":"2015-05-30T19:50:21Z",
            "_links":{  
               "self":"https://api.twitch .tv/kraken/users/feddecampo"
            },
            "display_name":"FeddeCampo",
            "logo":"http://static- cdn.jtvnw.net/jtv_user_pictures/feddecampo-profile_image-d4f7de23b28e0a86-300x30 0.jpeg",
            "bio":"Playstation MVP Sony and PS fan ",
            "type":"user"
         }
      }
   ],
   "_total":2315,
   "_links":{  
      "self":"https://api.twitch.tv/kraken/channels/greatbritishbg/follows?d irection=DESC&limit=1&offset=0",
      "next":"https://api.twitch.tv/kraken/channels/gr eatbritishbg/follows?direction=DESC&limit=1&offset=1"
   }
}

可以通过正常的js方式获取属性:

var displayName = response.follows[0].user.display_name

【讨论】:

  • 有没有办法把它变成一个字符串并在另一个函数中调用它?
  • 把它变成一个字符串?为什么?在另一个函数中调用它是什么意思?
  • 我需要像这样在 nodejs 中返回输出:client.say(channel, 'user.display_name ' + user.username + '!'); 在不同的函数中。
  • 这更没有意义!您已经“在 nodejs 中”-您可以提供更多上下文吗?你可以完全按照你说的去做:nt.say(channel, 'user.display_name ' + response.follows[0].user.display_name + '!');
  • Alex,我正在那个 .js 文件中的另一个函数中执行此操作。其他功能:client.addListener('chat', function (channel, user, message) { I NEED IT HERE });
猜你喜欢
  • 2015-03-16
  • 1970-01-01
  • 2016-04-17
  • 2015-06-28
  • 2018-10-27
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
相关资源
最近更新 更多