Dot-Net

使用 json.net 反序列化對象列表

  • February 22, 2021

我有這門課

public class Image
{
   public string url { get; set; }
   public string url_40px { get; set; }
   public string url_50px { get; set; }
}

public class Category
{
   public List<int> ancestor_ids { get; set; }
   public int parent_id { get; set; }
   public List<object> children_ids { get; set; }
   public string nodename { get; set; }
   public int num_parts { get; set; }
   public List<Image> images { get; set; }
   public string __class__ { get; set; }
   public int id { get; set; }
}

我像這樣反序列化它

retObject = JsonConvert.DeserializeObject(Of Category)(jsonResp)

但是對於返回的類別列表,我如何轉換為List<Category>?謝謝

的類型參數DeserializeObject必須是List<Category>而不是Category

在 C# 中它會是JsonConvert.DeserializeObject<List<Category>>(json)

在 VB.Net 中它會是JsonConvert.DeserializeObject(Of List(Of Category))(json)

引用自:https://stackoverflow.com/questions/11220776