C # Json 역 직렬화 실패

Bjørn Ørving

JSON rest api C #을 통해 로컬 TV 가이드를 표시 할 수있는 애플리케이션을 만들려고하는데 내 Json 응답의 역 직렬화에 몇 가지 문제가 있습니다.

나는 json 호출에서 지금 어레이에서 가장 열성적이며 API 서비스를 소유하지 않습니다.

이 오류 메시지가 나타납니다.

Newtonsoft.Json.DLL에서 'Newtonsoft.Json.JsonSerializationException'유형의 예외가 발생했지만 사용자 코드에서 처리되지 않았습니다.

추가 정보 : 유형에 JSON 배열이 필요하므로 현재 JSON 객체 (예 : { "name": "value"})를 'System.Collections.Generic.List`1 [TDC_Play_TV_Mobil.superclass2 + Now]'유형으로 역 직렬화 할 수 없습니다 (예 : [1,2,3])을 사용하여 올바르게 역 직렬화합니다.

이 오류를 수정하려면 JSON을 JSON 배열 (예 : [1,2,3])로 변경하거나 역 직렬화 된 유형을 변경하여 일반 .NET 유형이되도록 변경하십시오 (예 : 정수와 같은 기본 유형이 아니라 다음과 같은 컬렉션 유형이 아님). JSON 개체에서 역 직렬화 할 수있는 배열 또는 목록)입니다. JsonObjectAttribute를 유형에 추가하여 JSON 객체에서 강제로 역 직렬화 할 수도 있습니다.

경로 '지금', 줄 1, 위치 7

내 기능 :

private HttpClient client;

   public async Task<List<superclass2.Now>> GetComments()
   {
       client = new HttpClient();
      var response = await client.GetAsync(new Uri("http://api.yousee.tv/rest/tvguide/nowandnext/"));
       if (response.IsSuccessStatusCode)
       {
           
           string json = await response.Content.ReadAsStringAsync();

           System.Diagnostics.Debug.WriteLine(json);
           var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<List<superclass2.Now>>(json));

           var value = await task;
         //  List<superclass2.Now> comments = Newtonsoft.Json.JsonConvert.DeserializeObject<List<superclass2.Now>>(await json);
          System.Diagnostics.Debug.WriteLine("Comments er lavet");
          return value;

       }
       else
       {
           throw new Exception("Errorhandling message");

       }
        

   }

내 수업 파일 :

public class superclass2
{
public class Logos
   {
       public string small { get; set; }
       public string large { get; set; }
       public string small_seapp { get; set; }
       public string large_seapp { get; set; }
       public string extralarge { get; set; }
       public string super { get; set; }
       public string mega { get; set; }
       public string netgem { get; set; }
       public string svg { get; set; }
   }

   public class ChannelInfo
   {
       public string name { get; set; }
       public string shortname { get; set; }
       public string logo_image_prefix { get; set; }
       public Logos logos { get; set; }
       public int archivedays { get; set; }
       public string channelcolor { get; set; }
   }

   public class FormattedDate
   {
       public string time_begin { get; set; }
       public string time_end { get; set; }
       public string date { get; set; }
   }

   public class ImagesSixteenbynine
   {
       public string large { get; set; }
       public string medium { get; set; }
       public string small { get; set; }
   }

   public class ImagesFourbythree
   {
       public string large { get; set; }
       public string small { get; set; }
   }

   public class ImagesSquare
   {
       public string large { get; set; }
       public string medium { get; set; }
       public string small { get; set; }
   }

   public class Now
   {
       public int id { get; set; }
       public int dvb_id { get; set; }
       public int channel { get; set; }
       public ChannelInfo channel_info { get; set; }
       public string orgtitle { get; set; }
       public string cast { get; set; }
       public string directors { get; set; }
       public string series_info { get; set; }
       public int series_id { get; set; }
       public string series_name { get; set; }
       public bool allowseriesrecording { get; set; }
       public int totalinarchive { get; set; }
       public int popularity_score { get; set; }
       public int totalupcoming { get; set; }
       public int category { get; set; }
       public int subcategory { get; set; }
       public string category_string { get; set; }
       public string subcategory_string { get; set; }
       public int begin { get; set; }
       public object actual_begin { get; set; }
       public int end { get; set; }
       public int actual_end { get; set; }
       public int tvdate { get; set; }
       public FormattedDate formatted_date { get; set; }
       public string title { get; set; }
       public string description { get; set; }
       public bool archive { get; set; }
       public bool scrubbingallowed { get; set; }
       public int expiresfromarchive { get; set; }
       public bool startover { get; set; }
       public string imageprefix { get; set; }
       public ImagesSixteenbynine images_sixteenbynine { get; set; }
       public ImagesFourbythree images_fourbythree { get; set; }
       public ImagesSquare images_square { get; set; }
       public List<object> decorations { get; set; }
   }

   public class Logos2
   {
       public string small { get; set; }
       public string large { get; set; }
       public string small_seapp { get; set; }
       public string large_seapp { get; set; }
       public string extralarge { get; set; }
       public string super { get; set; }
       public string mega { get; set; }
       public string netgem { get; set; }
       public string svg { get; set; }
   }

   public class ChannelInfo2
   {
       public string name { get; set; }
       public string shortname { get; set; }
       public string logo_image_prefix { get; set; }
       public Logos2 logos { get; set; }
       public int archivedays { get; set; }
       public string channelcolor { get; set; }
   }

   public class FormattedDate2
   {
       public string time_begin { get; set; }
       public string time_end { get; set; }
       public string date { get; set; }
   }

   public class ImagesSixteenbynine2
   {
       public string large { get; set; }
       public string medium { get; set; }
       public string small { get; set; }
   }

   public class ImagesFourbythree2
   {
       public string large { get; set; }
       public string small { get; set; }
   }

   public class ImagesSquare2
   {
       public string large { get; set; }
       public string medium { get; set; }
       public string small { get; set; }
   }

   public class Next
   {
       public int id { get; set; }
       public int dvb_id { get; set; }
       public int channel { get; set; }
       public ChannelInfo2 channel_info { get; set; }
       public string orgtitle { get; set; }
       public string cast { get; set; }
       public string directors { get; set; }
       public string series_info { get; set; }
       public int series_id { get; set; }
       public string series_name { get; set; }
       public bool allowseriesrecording { get; set; }
       public int totalinarchive { get; set; }
       public int popularity_score { get; set; }
       public int totalupcoming { get; set; }
       public int category { get; set; }
       public int subcategory { get; set; }
       public string category_string { get; set; }
       public string subcategory_string { get; set; }
       public int begin { get; set; }
       public int actual_begin { get; set; }
       public int end { get; set; }
       public int actual_end { get; set; }
       public int tvdate { get; set; }
       public FormattedDate2 formatted_date { get; set; }
       public string title { get; set; }
       public string description { get; set; }
       public bool archive { get; set; }
       public bool scrubbingallowed { get; set; }
       public int expiresfromarchive { get; set; }
       public bool startover { get; set; }
       public string imageprefix { get; set; }
       public ImagesSixteenbynine2 images_sixteenbynine { get; set; }
       public ImagesFourbythree2 images_fourbythree { get; set; }
       public ImagesSquare2 images_square { get; set; }
       public List<object> decorations { get; set; }
   }

   public class RootObject
   {
       public List<Now> now { get; set; }
       public List<Next> next { get; set; }
   }

}
dbc

귀하의 JSON은 아니다 List<superclass2.Now>그것은이다, superclass2.RootObject. 따라서 다음과 같이해야합니다.

    public async Task<List<superclass2.Now>> GetComments()    
    {
        client = new HttpClient();
        var response = await client.GetAsync(new Uri("http://api.yousee.tv/rest/tvguide/nowandnext/"));
        if (response.IsSuccessStatusCode)
        {
            string json = await response.Content.ReadAsStringAsync();
            System.Diagnostics.Debug.WriteLine(json);
            var task = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<superclass2.RootObject>(json));

            var value = await task;
            System.Diagnostics.Debug.WriteLine("Comments er lavet");
            return (value == null ? null : value.now);
        }
        else
        {
            throw new Exception("Errorhandling message");
        }
    }

Jon Skeet과 동의하면 순전히 가독성 향상을 위해 클래스 중첩을 제거하는 것이 더 좋을 것입니다. 이러한 클래스가 특정 범위에만 존재해야하는 경우 중첩 된 네임 스페이스 를 사용하는 것이 좋습니다 . 그러나 버그를 일으키는 것은 아닙니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

JsonParseException : JSON 역 직렬화 실패

분류에서Dev

JSON C # 역 직렬화

분류에서Dev

c # Json 역 직렬화 식

분류에서Dev

C # : JSON 역 직렬화

분류에서Dev

JSON C # 역 직렬화

분류에서Dev

JSON C # 역 직렬화

분류에서Dev

Alexa Skill 요청 역 직렬화 실패-SkillRequest 개체에 대한 JSON C #

분류에서Dev

C # JSON 내에서 JSON 역 직렬화

분류에서Dev

C # Json 역 직렬화가 json 문자열의 첫 번째 문자에서 실패 함

분류에서Dev

WCF WebInvoke JSON 역 직렬화 실패-400 잘못된 요청

분류에서Dev

Azure JSON 응답 역 직렬화 C #

분류에서Dev

C #으로 json 역 직렬화

분류에서Dev

c #-특정 JSON의 역 직렬화

분류에서Dev

C #에서 Json 역 직렬화

분류에서Dev

C #에서 JSON 배열 역 직렬화

분류에서Dev

JSON을 C # 개체로 역 직렬화

분류에서Dev

JSON 역 직렬화 구문 분석 C #

분류에서Dev

JSON 데이터 역 직렬화 C #

분류에서Dev

C #에서 JSON 개체 역 직렬화

분류에서Dev

C # JSON 데이터 역 직렬화

분류에서Dev

Json 응답 C # 역 직렬화

분류에서Dev

특성없는 C # JSON 직렬화 및 역 직렬화

분류에서Dev

C #에서 JSON 파일 역 직렬화 및 직렬화

분류에서Dev

JSON 역 직렬화

분류에서Dev

C # 역 직렬화

분류에서Dev

C # / JSON 개체 직렬화, 역 직렬화 및 대 / 소문자 문제 없음

분류에서Dev

C #에서 JSON으로 직렬화하고 TS에서 역 직렬화

분류에서Dev

RestSharp로 JSON 역 직렬화

분류에서Dev

JSON 개체 역 직렬화