gpt4 book ai didi

c# - Json RestSharp 反序列化响应数据 null

转载 作者:太空狗 更新时间:2023-10-29 20:56:24 25 4
gpt4 key购买 nike

我使用 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"
}

最佳答案

根据 documentation ,RestSharp 仅反序列化为属性并且您正在使用字段。

RestSharp uses your class as the starting point, looping through each publicly-accessible, writable property and searching for a corresponding element in the data returned.

您需要将您的 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; }
}

关于c# - Json RestSharp 反序列化响应数据 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31051021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com