【发布时间】:2015-06-26 14:30:14
【问题描述】:
我使用 RestSharp 访问 Rest API。我喜欢将数据作为 POCO 取回。 我的 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"
}
【问题讨论】:
-
我路过这个。就我而言,我创建了一个带参数的新构造函数,但我忘记了它创建了一个不带参数的构造函数。