2017-05-17 3 views
0

JSON REST-api 요청을 가져 오려고하는데 Newtonsoft.Json 패키지를 사용하여 비 직렬화합니다. .NET 및 액세스 새로 만든 객체 내 방법,하지만 난 내 비주얼 스튜디오 다음 JSON 문자열의 경우 2015 년개체의 메서드가 호출 될 때 C# 개체에 대한 비 직렬화 JSON 문자열이 "정적이 아닌 컨텍스트에서 정적 클래스에 액세스 할 수 없습니다"라는 메시지를 표시합니다.

내 C# 코드를 실행하지 않습니다 오류가 점점 계속에 대한

{ 
    "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 class에서 GET 요청을 성공적으로 가져 와서 문자열에 저장했습니다 (jsonstring). 지금은이 코드를 실행하려고 :

Discogs myUser = new Discogs(); 
myUser = JsonConvert.DeserializeObject<Discogs>(jsonstring); 
int yr = myUser.pagination.data.year; 

을 ...하지만 내 프로젝트가 오류를 얻을, 객체 참조가 비 정적 필드, 메서드 또는 속성 'Discogs.pagination.data.year 필요 ', 은 정적 컨텍스트에서'year '비 정적 속성에 액세스 할 수 없습니다.

정적 클래스 또는 메서드가 없기 때문에 이것은 내게 의미가 없습니다. 솔루션을 찾았지만 비슷한 문제가 있으면 이러한 오류없이 deserialize 된 객체에 액세스 할 수있는 것 같습니다. Discogs 객체의 메서드에 액세스하는 데 도움이되는 점은 크게 감사하겠습니다.

답변

1

클래스를 인스턴스화하지 않고 Pagination 클래스를 사용하려고하면 문제가 발생합니다. 비 정적 클래스 (매김, 데이터, 커뮤니티)을 사용하려면 먼저 여기에 귀하의 구조는 꽤 이상한

Pagination pag = new Pagination(); 

아래처럼 그것들을 초기화해야합니다. 일반적으로 클래스는 별도의 파일에 있거나 적어도 여기에있는 것처럼 중첩되지 않습니다. 이 프로그램을 설계 한 방식을 다시 생각해 볼 수 있습니다.

+0

와우. 나는 우리가 중첩 된 수업을 가질 수 있는지조차 몰랐다. C#이이 기능을 지원한다는 것을 알지 못했습니다. – Alisson

+0

와우 감사합니다. 나는 몇 분 안에 내가 처음 몇 시간 동안 붙어 있었던 문제를 해결 한 첫 번째 대답을 믿을 수 없다. 다른 클래스에 중첩 된 클래스도 인스턴스화해야한다는 것을 알지 못했습니다. 처음에는 자신에게 도전하고 json 쿼리에 대한 객체를 처음부터 만들고 싶었지만 조언을 구한 후 [json2csharp] (http://json2csharp.com/)를 사용하여 객체를 자동 생성했습니다. 중첩 된 클래스가 필요없이 내 전체 구조를 재정렬하고 이제 모든 데이터가 올바르게 표시됩니다. 다시 한번 고마워요 !! : D –

1

"myUser.pagination ..."에서 값을 얻으려고 시도하고 있지만 예제에서 "페이지 매김"은 "디스 파그"클래스의 속성이 아니라 "페이지 매김"클래스의 "데이터"와 같은 클래스 이름입니다 중첩 된 클래스와

코드 : 내가 가진

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} 
} 
+0

Tim의 답변을 확대 해 주셔서 감사합니다! 나는 당신의 대답 때문에 중첩 된 클래스에 대해 훨씬 더 잘 이해하고있다. 감사! –