繁体   English   中英

无法访问json类上的变量。 C#

[英]Can't access variables on a json class. C#

我最近尝试使用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;

StreamTwitchAPI中一个的名称-就我们TwitchAPI ,您根本没有在TwitchAPI类中声明任何字段。 因此,您可以使用:

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

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

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

如果要访问Stream则需要创建一个实例。

如果您想以自己的方式访问,请在TwitchApi类下创建一个属性,如下所示

public Stream Stream
{
    get;
    set;
}

然后,您可以按以下方式访问它:

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

此外,除非计划要实现某些特定的目标,否则可以避免使用嵌套类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM