我一直在尝试使用RestSharp将这个json反序列化两天。我浏览了RestSharp github网站,查看了无数示例,并在Stack Overflow上花了很多时间尝试找到无济于事的答案。我的代码以前运行良好,但是供应商更改了API版本,因此我不得不进行更新以继续使用该应用程序进行法律实践。我的json如下(客户端信息已删除,并替换为通用信息):
{
"data":[
{
"id":1035117666,
"client":
{
"id":905422394,
"name":"client1"
},
"display_number":"11-00012",
"description":"General",
"practice_area":
{
"id":4269978,
"name":"Business"
},
"status":"Open",
"open_date":"2011-12-14",
"close_date":null,
"billing_method":"hourly"
},
{
"id":1035117768,
"client":
{
"id":905422506,
"name":"client2"
},
"display_number":"12-00037",
"description":"HOA",
"practice_area":
{
"id":4269978,
"name":"Business"
},
"status":"Open",
"open_date":"2012-08-07",
"close_date":null,
"billing_method":"hourly"
}
],
"meta":
{
"paging":
{
"next":"https://app.goclio.com/api/v4/matters.json?fields=id%2C+client%7Bid%2C+name%7D%2C+display_number%2C+description%2C+practice_area%7Bid%2C+name%7D%2C+status%2C+open_date%2C+close_date%2C+billing_method&limit=2&page_token=BAh7BjoLb2Zmc2V0aQc%3D--b1ea3eba20c8acefbcdfc7868debd1e0ee630c64&status=Open"
},
"records":91
}
}
我在c#代码中构建了以下架构:
public class MatterList
{
public List<Matter> matters { get; set; }
public Meta meta { get; set; }
}
public class Meta
{
public Paging paging { get; set; }
public int records { get; set; }
}
public class Paging
{
public string previous { get; set; }
public string next { get; set; }
}
[DeserializeAs(Name = "data")]
public class Matter
{
public int id { get; set; }
public Client client { get; set; }
public string display_number { get; set; }
public string description { get; set; }
public PracticeArea practice_area { get; set; }
public string status { get; set; }
public DateTime open_date { get; set; }
public DateTime close_date { get; set; }
public string billing_method { get; set; }
public string type = "matter";
}
public class PracticeArea
{
public int id { get; set; }
public string name { get; set; }
}
public class Client
{
public int id { get; set; }
public string name { get; set; }
}
当我运行RestSharp反序列化方法时,我正在使用以下代码行将结果发送到MatterList类型的对象
MatterList matterList = jsonHandler.Deserialize<MatterList>(response);
到目前为止,我一直尝试在没有Meta或Paging POCO类的情况下反序列化,同时对MatterList类进行相应的更改(删除Meta属性)。
我尝试使用和不使用[DeserializeAs(Name =“ data”)]指令。
我试图在反序列化之前设置json响应的RootElement。
我试图通过将其与Execute请求代码结合使用来简化反序列化
IRestResponse<MatterList> matterList = client.Execute<MatterList>(request);
我创建了一个名为MatterContainer的容器类,将其放置在架构中的MatterList和Matter类之间:
public class MatterList
{
public List<MatterContainer> matters { get; set; }
}
public class MatterContainer
{
public Matter matter { get; set; }
}
public class Matter
{
public int id { get; set; }
public Client client { get; set; }
public string display_number { get; set; }
public string description { get; set; }
public PracticeArea practice_area { get; set; }
public string status { get; set; }
public DateTime open_date { get; set; }
public DateTime close_date { get; set; }
public string billing_method { get; set; }
public string type = "matter";
}
我知道我从服务器正确地获取了json响应,因此我的请求是正确的,反序列化后MatterList不为null。问题是我无法反序列化实际填充MatterList类中的List问题。
我已经断断续续地看了两天,无法克服这个障碍。如果有人发现我做错了,我将不胜感激,我的观点是我无法继续进行申请。
谢谢!
答案 0 :(得分:1)
我认为您的[DeserializeAs(Name = "data")]
属性在错误的位置。尝试将其放在根类中:
public class MatterList
{
[DeserializeAs(Name = "data")]
public List<Matter> matters { get; set; }
public Meta meta { get; set; }
}
或者,尝试将该属性重命名为data