将Json列表反序列化为C#List对象
我有一个返回json字符串的类,但我想将其反序列化为C#List对象。我当前的代码如下所示将Json列表反序列化为C#List对象,c#,.net,json,deserialization,C#,.net,Json,Deserialization,我有一个返回json字符串的类,但我想将其反序列化为C#List对象。我当前的代码如下所示 public class JsonBuilder { public static string BuildJson(DateTime fromDate, DateTime toDate) { var list = new List<dynamic>(); // create list with json o
public class JsonBuilder
{
public static string BuildJson(DateTime fromDate, DateTime toDate)
{
var list = new List<dynamic>();
// create list with json object from service
var jsonObjList = JsonConvert.SerializeObject(list);
var des = (List<JsonObject>)JsonConvert.DeserializeObject(jsonObjList, typeof(List<JsonObject>));
return JsonConvert.SerializeObject(list);
}
你试过这个吗:
var des = (List<DeserializeObjects>)JsonConvert.DeserializeObject(jsonObjList, jsonObjList.GetType()));
var des=(List)JsonConvert.DeserializeObject(jsonObjList,jsonObjList.GetType());
或者这个:
var des = (List<DeserializeObjects>)JsonConvert.DeserializeObject(jsonObjList, typeof(List<dynamic>));
var des=(List)JsonConvert.DeserializeObject(jsonObjList,typeof(List));
否则,这篇文章也可以帮助你实现你的目标:
试试这个
var jsonObjList = JsonConvert.SerializeObject(list);
dynamic resultList = JsonConvert.DeserializeObject(jsonObjList);
我会尝试使用这个
类
public class Author
{
public int id { get; set; }
public string slug { get; set; }
public string name { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string nickname { get; set; }
public string url { get; set; }
public string description { get; set; }
}
public class CustomFields
{
public List<string> tags { get; set; }
}
public class Post
{
public int id { get; set; }
public string type { get; set; }
public string slug { get; set; }
public string url { get; set; }
public string status { get; set; }
public string title { get; set; }
public string title_plain { get; set; }
public string content { get; set; }
public string excerpt { get; set; }
public string date { get; set; }
public string modified { get; set; }
public List<object> categories { get; set; }
public List<object> tags { get; set; }
public Author author { get; set; }
public List<object> comments { get; set; }
public List<object> attachments { get; set; }
public int comment_count { get; set; }
public string comment_status { get; set; }
public CustomFields custom_fields { get; set; }
}
public class YourObject
{
public string status { get; set; }
public int count { get; set; }
public int count_total { get; set; }
public int pages { get; set; }
public List<Post> posts { get; set; }
}
公共类作者
{
公共int id{get;set;}
公共字符串slug{get;set;}
公共字符串名称{get;set;}
公共字符串first_name{get;set;}
公共字符串last_name{get;set;}
公共字符串昵称{get;set;}
公共字符串url{get;set;}
公共字符串说明{get;set;}
}
公共类自定义字段
{
公共列表标记{get;set;}
}
公营职位
{
公共int id{get;set;}
公共字符串类型{get;set;}
公共字符串slug{get;set;}
公共字符串url{get;set;}
公共字符串状态{get;set;}
公共字符串标题{get;set;}
公共字符串标题\u plain{get;set;}
公共字符串内容{get;set;}
公共字符串摘录{get;set;}
公共字符串日期{get;set;}
公共字符串已修改{get;set;}
公共列表类别{get;set;}
公共列表标记{get;set;}
公共作者{get;set;}
公共列表注释{get;set;}
公共列表附件{get;set;}
public int comment_count{get;set;}
公共字符串注释_状态{get;set;}
公共自定义字段自定义_字段{get;set;}
}
公共类对象
{
公共字符串状态{get;set;}
公共整数计数{get;set;}
公共整数计数\u总计{get;set;}
公共整型页{get;set;}
公共列表发布{get;set;}
}
然后,使用以下命令反序列化:
var yourObject = JsonConvert.DeserializeObject<YourObject>(json);
var yourObject=JsonConvert.DeserializeObject(json);
不幸的是,这些技巧对我都不起作用。有什么建议吗?我从你提供的链接中找到了一个解决方案。谢谢。
var yourObject = JsonConvert.DeserializeObject<YourObject>(json);