我遇到了使用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);
是否有某个地方我犯了错误?
答案 0 :(得分:2)
那是因为你的client:{}
被包装在JSON对象中。最简单的方法是为C#Client
类添加一些包装器。
public class Container
{
public Client Client { get; set; }
}
...
var response = client.Execute<Container>(request);