C#Json反序列化失败

比昂·奥尔文

我正在尝试制作一个可以通过JSON rest api C#显示本地电视指南的应用程序,但是在反序列化我的Json响应时遇到了一些问题。

我现在在json调用中只对数组感兴趣,并且我不拥有api服务。

我收到此错误消息:

Newtonsoft.Json.DLL中发生了类型为'Newtonsoft.Json.JsonSerializationException'的异常,但未在用户代码中处理

附加信息:无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型'System.Collections.Generic.List`1 [TDC_Play_TV_Mobil.superclass2 + Now]',因为该类型需要JSON数组(例如[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; }
   }

}
数据库

您的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

反序列化对象的Arraylist

분류에서Dev

使用GSON自定义反序列化枚举

분류에서Dev

Json编码或序列化XML

분류에서Dev

子类的C#XML序列化-从根节点删除xmlns:p1和p1:type属性

분류에서Dev

使用jdbc-message-channel的序列化错误

분류에서Dev

protobuf-net:回调,用于确定要序列化的对象

분류에서Dev

REST错误响应和客户端服务器POJO序列化

분류에서Dev

C#-将对象列表序列化为文件的最佳方法是什么?

분류에서Dev

用请求数据覆盖序列化程序,该请求数据包括缺少键的空值

분류에서Dev

C ++中的初始化程序列表和类初始化。我有无参数构造函数,但仍必须使用初始化列表?

분류에서Dev

Как предотвратить инициализацию свойств, которых нет в строке JSON во время десериализации в C #?

분류에서Dev

从int转换为c字符串(const char *)失败

분류에서Dev

C# JSON & Parsing

분류에서Dev

JSON parser tip in C

분류에서Dev

Tropo C # JSON 예제?

분류에서Dev

C ++ JSON 직렬화

분류에서Dev

C #의 json 형식

분류에서Dev

Turn string into json C#

분류에서Dev

Read Json data C#

분류에서Dev

c # json newtonsoft 변환

분류에서Dev

c # json pull 요청

분류에서Dev

JSON (Windows 10 Universal C #)

분류에서Dev

Newtonsoft.Json의 JSON C # DeserializeObject 오류

분류에서Dev

C # JSON 내에서 JSON 역 직렬화

분류에서Dev

自動によるC ++ 11の初期化

분류에서Dev

C结构初始化(整数和整数值的数组)

분류에서Dev

URL 및 회 문의 C # JSON

분류에서Dev

Deserializing weirdly formatted JSON to an object in C#

분류에서Dev

JSON C # 역 직렬화