Restsharp 没有反序列化响应

Restsharp is not deserializing response

我在使用 RestSharp 反序列化响应时遇到问题。我收到以下 JSON 响应

{
  "client": {
    "clientnr":"1",
    "contact":" Johnny Bravo",
    "showcontact":"false",
    "company":"Acme International",
    "address":"Washington Street 250",
    "zipcode":"1234567",
    "city":"SOMECITYNAME",
    "country":"146",
    "phone":"0048122123123",
    "mobile":"myMobileNumber",
    "email":"some@email.com",
    "bankcode":"",
    "biccode":"",
    "taxnumber":"NL001234567B01",
    "tax_shifted":"true",
    "lastinvoice":"",
    "sendmethod":"email",
    "paymentmethod":"bank",
    "top":"0",
    "stddiscount":"0",
    "mailintro":"",
    "reference": {
      "line1":"",
      "line2":null,
      "line3":null
     },
    "notes":"",
    "notes_on_invoice":"false",
    "active":"true",
    "default_doclang":"en",
    "default_email":"0",
    "currency":"EUR",
    "mandate_id":"",
    "mandate_date":"",
    "collecttype":"FRST",
    "tax_type":"extax",
    "default_category":"0"
  }
}   

我使用 JSON 进行 csharp 以创建 类

public class Reference
{
    public string line1 { get; set; }
    public object line2 { get; set; }
    public object line3 { get; set; }
}

public class Client
{
    public string clientnr { get; set; }
    public string contact { get; set; }
    public string showcontact { get; set; }
    public string company { get; set; }
    public string address { get; set; }
    public string zipcode { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string phone { get; set; }
    public string mobile { get; set; }
    public string email { get; set; }
    public string bankcode { get; set; }
    public string biccode { get; set; }
    public string taxnumber { get; set; }
    public string tax_shifted { get; set; }
    public string lastinvoice { get; set; }
    public string sendmethod { get; set; }
    public string paymentmethod { get; set; }
    public string top { get; set; }
    public string stddiscount { get; set; }
    public string mailintro { get; set; }
    public Reference reference { get; set; }
    public string notes { get; set; }
    public string notes_on_invoice { get; set; }
    public string active { get; set; }
    public string default_doclang { get; set; }
    public string default_email { get; set; }
    public string currency { get; set; }
    public string mandate_id { get; set; }
    public string mandate_date { get; set; }
    public string collecttype { get; set; }
    public string tax_type { get; set; }
    public string default_category { get; set; }

} 

但是每次调用 API 时,我都会得到 null(因此反序列化似乎不起作用)。 我一直在调试它,所以我会发现一些错误,但是这个过程很顺利,没有任何错误。

我的调用代码如下

var client = new RestClient("https://www.myapi.com/");
var request = new RestRequest("api/xyz/something", Method.GET);
var response2 = client.Execute<Client>(request);

我是不是哪里做错了?

那是因为您的 client:{} 包裹在 JSON 对象中。最简单的方法是为您的 C# 添加一些包装器 Client class。

public class Container
{
    public Client Client { get; set; }
}

...

var response = client.Execute<Container>(request);