【问题标题】:Can't access variables on a json class. C#无法访问 json 类上的变量。 C#
【发布时间】:2016-04-30 15:18:21
【问题描述】:

我最近尝试使用 twitch json api,我从来没有使用过 json,所以这是一个很好的挑战,但后来我使用了 json2csharp.com 并将 json 字符串转换为这样的类:

class TwitchAPI
{
    public class Preview
    {
        public string small { get; set; }
        public string medium { get; set; }
        public string large { get; set; }
        public string template { get; set; }
    }

    public class Links
    {
        public string self { get; set; }
        public string follows { get; set; }
        public string commercial { get; set; }
        public string stream_key { get; set; }
        public string chat { get; set; }
        public string features { get; set; }
        public string subscriptions { get; set; }
        public string editors { get; set; }
        public string teams { get; set; }
        public string videos { get; set; }
    }

    public class Channel
    {
        public bool mature { get; set; }
        public string status { get; set; }
        public string broadcaster_language { get; set; }
        public string display_name { get; set; }
        public string game { get; set; }
        public string language { get; set; }
        public int _id { get; set; }
        public string name { get; set; }
        public string created_at { get; set; }
        public string updated_at { get; set; }
        public object delay { get; set; }
        public string logo { get; set; }
        public object banner { get; set; }
        public string video_banner { get; set; }
        public object background { get; set; }
        public string profile_banner { get; set; }
        public object profile_banner_background_color { get; set; }
        public bool partner { get; set; }
        public string url { get; set; }
        public int views { get; set; }
        public int followers { get; set; }
    }

    public class Stream
    {
        public long _id { get; set; }
        public string game { get; set; }
        public int viewers { get; set; }
        public string created_at { get; set; }
        public int video_height { get; set; }
        public int average_fps { get; set; }
        public int delay { get; set; }
        public bool is_playlist { get; set; }
    }
}

现在我尝试访问查看器,但它不允许我访问。我就是这样做的。

TwitchAPI twitchapi = new TwitchAPI();
string viewers = "" + twitchapi.Stream.viewers;

【问题讨论】:

    标签: c# json twitch json2csharp


    【解决方案1】:

    StreamTwitchAPI 中的 的名称——据我们所知,您根本没有在TwitchAPI 类中声明任何字段。所以你可以使用:

    TwitchAPI.Stream stream = new TwitchAPI.Stream();
    string viewers = stream.viewers.ToString();
    

    ...但目前没有与TwitchAPI 实例关联的流。

    (顺便说一句,我相信有很多可用的 Twitter API 客户端......如果您的目标是使用 Twitter 做某事而不是构建自己的 Twitter API,我建议使用现有的。)

    【讨论】:

    • 谢谢,我只是为了学习才尝试做这个,我才 14 岁,所以我认为这将是一个很好的机会。
    【解决方案2】:

    如果你想访问Stream,你需要创建一个实例。

    如果你想像你一样访问,那么像这样在 TwitchApi 类下创建一个属性

    public Stream Stream
    {
        get;
        set;
    }
    

    然后你可以访问它,

    TwitchAPI twitchapi = new TwitchAPI();
    string viewers = "" + twitchapi.Stream.viewers;
    

    此外,您可以避免嵌套类,除非您计划实现特定目标。

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 2017-10-07
      • 1970-01-01
      • 2014-06-04
      • 2022-11-18
      • 1970-01-01
      • 2014-08-22
      • 2023-03-27
      • 2015-12-07
      相关资源
      最近更新 更多