Json RestSharp正在消除响应数据为空的干扰
本文关键字:数据 干扰 响应 RestSharp Json | 更新日期: 2023-09-27 17:59:36
我使用RestSharp访问Rest API。我喜欢以POCO的身份取回Data。我的RestSharp客户端看起来像这样:
var client = new RestClient(@"http:''localhost:8080");
var request = new RestRequest("todos/{id}", Method.GET);
request.AddUrlSegment("id", "4");
//request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
//With enabling the next line I get an new empty object of TODO
//as Data
//client.AddHandler("*", new JsonDeserializer());
IRestResponse<ToDo> response2 = client.Execute<ToDo>(request);
ToDo td=new JsonDeserializer().Deserialize<ToDo>(response2);
var name = response2.Data.name;
JsonObject的类如下所示:
public class ToDo
{
public int id;
public string created_at;
public string updated_at;
public string name;
}
Json响应:
{
"id":4,
"created_at":"2015-06-18 09:43:15",
"updated_at":"2015-06-18 09:43:15",
"name":"Another Random Test"
}
根据文档,RestSharp只反序列化为属性,而您使用的是字段。
RestSharp使用您的类作为起点,循环遍历每个类可公开访问、可写的属性,并搜索返回的数据中的相应元素。
您需要将ToDo
类更改为以下内容:
public class ToDo
{
public int id { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string name { get; set; }
}