Restsharp Deserializer returns 对象数组的空属性
Restsharp Deserializer returns empty properties of Array of Objects
我要返回一个 JSON,其中包含我创建的 API 中的对象数组。
[{"Beneficiary":"QaiTS","Total":1000.00,"CurrencyCode":"PHP"}, {"Beneficiary":"MANILEÑOS","Total":4500.00,"CurrencyCode":"PHP"}]
我正在尝试使用 Restsharp 的反序列化器对其进行反序列化,但是当我打印出列表时,它显示属性为空。
我的代码如下所示:
var client = new RestClient("http://localhost:4000/api/payments/GetPaymentSummary");
var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Json;
var response = client.Execute<List<PaymentSummary>>(request);
JsonDeserializer deserialize = new JsonDeserializer();
List<PaymentSummary> list = deserialize.Deserialize<List<PaymentSummary>>(response);
我在输出时打印的结果:
Beneficiary:
CurrencyCode:
Total: 0
Beneficiary:
CurrencyCode:
Total: 0
编辑:这是 PaymentSummary class 的样子:
public class PaymentSummary
{
public string Beneficiary;
public decimal Total;
public string CurrencyCode;
}
您的 class 当前由 public 个字段组成。 RestSharp 不会反序列化为字段,只有 public 属性。您需要将其更新为以下内容:
public class PaymentSummary
{
public string Beneficiary { get; set; }
public decimal Total { get; set; }
public string CurrencyCode { get; set; }
}
我要返回一个 JSON,其中包含我创建的 API 中的对象数组。
[{"Beneficiary":"QaiTS","Total":1000.00,"CurrencyCode":"PHP"}, {"Beneficiary":"MANILEÑOS","Total":4500.00,"CurrencyCode":"PHP"}]
我正在尝试使用 Restsharp 的反序列化器对其进行反序列化,但是当我打印出列表时,它显示属性为空。
我的代码如下所示:
var client = new RestClient("http://localhost:4000/api/payments/GetPaymentSummary");
var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Json;
var response = client.Execute<List<PaymentSummary>>(request);
JsonDeserializer deserialize = new JsonDeserializer();
List<PaymentSummary> list = deserialize.Deserialize<List<PaymentSummary>>(response);
我在输出时打印的结果:
Beneficiary:
CurrencyCode:
Total: 0
Beneficiary:
CurrencyCode:
Total: 0
编辑:这是 PaymentSummary class 的样子:
public class PaymentSummary
{
public string Beneficiary;
public decimal Total;
public string CurrencyCode;
}
您的 class 当前由 public 个字段组成。 RestSharp 不会反序列化为字段,只有 public 属性。您需要将其更新为以下内容:
public class PaymentSummary
{
public string Beneficiary { get; set; }
public decimal Total { get; set; }
public string CurrencyCode { get; set; }
}