【问题标题】:Restsharp is not deserializing responseRestsharp 没有反序列化响应
【发布时间】:2015-03-02 02:41:13
【问题描述】:

我在使用 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);

有什么地方我犯了错误吗?

【问题讨论】:

    标签: json deserialization restsharp


    【解决方案1】:

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

    public class Container
    {
        public Client Client { get; set; }
    }
    
    ...
    
    var response = client.Execute<Container>(request);
    

    【讨论】:

    • 嘿!我发现答案是添加 request.RootElement = "client";这是最容易做到的:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多