将JSON字符串反序列化为C#对象时出现错误;无法在非静态上下文中访问静态类“;当对象';s方法被称为

将JSON字符串反序列化为C#对象时出现错误;无法在非静态上下文中访问静态类“;当对象';s方法被称为,c#,visual-studio-2015,json.net,C#,Visual Studio 2015,Json.net,我试图获取一个JSON REST api请求,使用Newtonsoft.JSON包for.NET和新创建的对象中的访问方法对其进行反序列化,但我不断收到一个错误,该错误不允许我在Visual Studio 2015中运行我的C#代码 对于以下JSON字符串 { "pagination": { "per_page": 1, "items": 28, "page": 1, "urls": {

我试图获取一个JSON REST api请求,使用Newtonsoft.JSON包for.NET和新创建的对象中的访问方法对其进行反序列化,但我不断收到一个错误,该错误不允许我在Visual Studio 2015中运行我的C#代码

对于以下JSON字符串

{
    "pagination": 
    {
        "per_page": 1,
        "items": 28, 
        "page": 1, 
        "urls": 
        {
            "last": "https://...",
            "next": "https://..."
        },
        "pages": 28
    },
    "results": 
    [{
        "style": ["House"],
        "thumb": "https://...", 
        "format": ["File", "AAC", "Album"], 
        "country": "Unknown", 
        "barcode": ["id886037928"], 
        "uri": "/Porter-Robinson-Worlds/master/721049", 
        "community": {"have": 932, "want": 720}, 
        "label": ["Astralwerks", "Sample Sized, LLC", "Astralwerks"], 
        "catno": "none", 
        "year": "2014", 
        "genre": ["Electronic"], 
        "title": "Porter Robinson - Worlds", 
        "resource_url": "https://...", 
        "type": "master", 
        "id": 721049
    }]
}
我创建了以下C#对象类:

public class Discogs
{
    public class pagination
    {
        public int per_page { get; set; }
        public int items { get; set; }
        public int page { get; set; }

        public class urls
        {
            public string last { get; set; }
            public string next { get; set; }

        }

        public int pages { get; set; }


        public class data
        {
            public string[] style { get; set; }
            public string thumb { get; set; }
            public string[] format { get; set; }
            public string country { get; set; }
            public string[] barcode { get; set; }
            public string uri { get; set; }

            public class community
            {
                public string have { get; set; }
                public string want { get; set; }
            }

            public string[] label { get; set; }
            public string catno { get; set; }
            public string year { get; set; }
            public string[] genre { get; set; }
            public string title { get; set; }
            public string resource_url { get; set; }
            public string type { get; set; }
            public string id { get; set; }
        }
        public class results
        {
            public data Results { get; set; }
        }
    }
}
在一个
private async void类中
,我成功地获取了GET请求并将其存储在字符串
jsonstring
中。现在我尝试运行以下代码:

Discogs myUser = new Discogs();
myUser = JsonConvert.DeserializeObject<Discogs>(jsonstring);
int yr = myUser.pagination.data.year;
Discogs myUser=new Discogs();
myUser=JsonConvert.DeserializeObject(jsonstring);
int yr=myUser.pagination.data.year;
…但我的项目出现错误,非静态字段、方法或属性“Discogs.pagination.data.year”需要对象引用,无法在静态上下文中访问非静态属性“year”


这对我来说没有意义,因为我没有静态类或方法。我已经寻找了一个解决方案,但所有类似的问题似乎都能够访问反序列化对象,而不会出现任何此类错误。如果您对访问我的Discogs对象中的方法有任何帮助,我们将不胜感激。

问题在于您试图使用类
分页
,而不实例化该类。为了使用非静态类(分页、数据、社区),您必须首先像下面那样实例化它们

Pagination pag = new Pagination();

你的结构很奇怪。通常情况下,类将位于单独的文件中,或者至少不会像这里那样嵌套。您可能需要重新考虑设计此程序的方式。

问题在于,您试图在不实例化类的情况下使用类
分页。为了使用非静态类(分页、数据、社区),您必须首先像下面那样实例化它们

Pagination pag = new Pagination();

你的结构很奇怪。通常情况下,类将位于单独的文件中,或者至少不会像这里那样嵌套。您可能需要重新考虑设计此程序的方式。

您试图从“myUser.pagination…”中获取值,但在您的示例中,“pagination”是一个类名,而不是“Discogs”类中的属性,与“pagination”类中的“data”相同

具有嵌套类的代码:

public class Discogs
{
    public class Pagination
    {
        public int per_page { get; set; }
        public int items { get; set; }
        public int page { get; set; }

        public class Urls
        {
            public string last { get; set; }
            public string next { get; set; }

        }

        public Urls urls {get;set;}

        public int pages { get; set; }

        public class Data
        {
            public string[] style { get; set; }
            public string thumb { get; set; }
            public string[] format { get; set; }
            public string country { get; set; }
            public string[] barcode { get; set; }
            public string uri { get; set; }

            public class Community
            {
                public string have { get; set; }
                public string want { get; set; }
            }

            public Community community { get; set; }

            public string[] label { get; set; }
            public string catno { get; set; }
            public string year { get; set; }
            public string[] genre { get; set; }
            public string title { get; set; }
            public string resource_url { get; set; }
            public string type { get; set; }
            public string id { get; set; }
        }

        public class Results
        {
            public Data Results { get; set; }
        }

        public Results result {get;set;}
    }

    public Pagination pagination {get;set}
} 
使用我认为有点容易理解的代码:

public class Urls
{
    public string last { get; set; }
    public string next { get; set; }

}

public class Community
{
    public string have { get; set; }
    public string want { get; set; }
}

public class Data
{
    public string[] style { get; set; }
    public string thumb { get; set; }
    public string[] format { get; set; }
    public string country { get; set; }
    public string[] barcode { get; set; }
    public string uri { get; set; }
    public string[] label { get; set; }
    public string catno { get; set; }
    public string year { get; set; }
    public string[] genre { get; set; }
    public string title { get; set; }
    public string resource_url { get; set; }
    public string type { get; set; }
    public string id { get; set; }

    public Community community { get; set; }
}

public class Results
{
    public Data Results { get; set; }
}       

public class Pagination
{
    public int per_page { get; set; }
    public int items { get; set; }
    public int page { get; set; }
    public int pages { get; set; }

    public Urls urls {get;set;}
    public Results result {get;set;}
}

public class Discogs
{
    public Pagination pagination {get;set}
} 

您试图从“myUser.pagination…”获取值,但在您的示例中,“pagination”是一个类名,而不是“Discogs”类中的属性,与“pagination”类中的“data”相同

具有嵌套类的代码:

public class Discogs
{
    public class Pagination
    {
        public int per_page { get; set; }
        public int items { get; set; }
        public int page { get; set; }

        public class Urls
        {
            public string last { get; set; }
            public string next { get; set; }

        }

        public Urls urls {get;set;}

        public int pages { get; set; }

        public class Data
        {
            public string[] style { get; set; }
            public string thumb { get; set; }
            public string[] format { get; set; }
            public string country { get; set; }
            public string[] barcode { get; set; }
            public string uri { get; set; }

            public class Community
            {
                public string have { get; set; }
                public string want { get; set; }
            }

            public Community community { get; set; }

            public string[] label { get; set; }
            public string catno { get; set; }
            public string year { get; set; }
            public string[] genre { get; set; }
            public string title { get; set; }
            public string resource_url { get; set; }
            public string type { get; set; }
            public string id { get; set; }
        }

        public class Results
        {
            public Data Results { get; set; }
        }

        public Results result {get;set;}
    }

    public Pagination pagination {get;set}
} 
使用我认为有点容易理解的代码:

public class Urls
{
    public string last { get; set; }
    public string next { get; set; }

}

public class Community
{
    public string have { get; set; }
    public string want { get; set; }
}

public class Data
{
    public string[] style { get; set; }
    public string thumb { get; set; }
    public string[] format { get; set; }
    public string country { get; set; }
    public string[] barcode { get; set; }
    public string uri { get; set; }
    public string[] label { get; set; }
    public string catno { get; set; }
    public string year { get; set; }
    public string[] genre { get; set; }
    public string title { get; set; }
    public string resource_url { get; set; }
    public string type { get; set; }
    public string id { get; set; }

    public Community community { get; set; }
}

public class Results
{
    public Data Results { get; set; }
}       

public class Pagination
{
    public int per_page { get; set; }
    public int items { get; set; }
    public int page { get; set; }
    public int pages { get; set; }

    public Urls urls {get;set;}
    public Results result {get;set;}
}

public class Discogs
{
    public Pagination pagination {get;set}
} 

哇!我甚至不知道我们可以有嵌套类。我不打算使用它,只是不知道C#支持这个功能。哇,非常感谢!!!我不敢相信第一个答案,几分钟内就解决了一个困扰了我几个小时的问题。我没有意识到嵌套在其他类中的类也必须实例化。我最初想挑战自己,从头开始为我的json查询创建一个对象,但我接受了您的建议,并使用自动创建了该对象。它重新安排了我的整个结构,而不需要嵌套类,现在我的所有数据都正确显示了。再次感谢大家!!:德沃。我甚至不知道我们可以有嵌套类。我不打算使用它,只是不知道C#支持这个功能。哇,非常感谢!!!我不敢相信第一个答案,几分钟内就解决了一个困扰了我几个小时的问题。我没有意识到嵌套在其他类中的类也必须实例化。我最初想挑战自己,从头开始为我的json查询创建一个对象,但我接受了您的建议,并使用自动创建了该对象。它重新安排了我的整个结构,而不需要嵌套类,现在我的所有数据都正确显示了。再次感谢大家!!:谢谢你对蒂姆的回答进行了详细的阐述!由于您现在的回答,我对嵌套类有了更好的理解。谢谢感谢您详细介绍Tim的答案!由于您现在的回答,我对嵌套类有了更好的理解。谢谢