简体   繁体   中英

Restsharp Deserializer returns empty properties of Array of Objects

I'm returning a JSON containing an array of objects from an API I made. [{"Beneficiary":"QaiTS","Total":1000.00,"CurrencyCode":"PHP"}, {"Beneficiary":"MANILEÑOS","Total":4500.00,"CurrencyCode":"PHP"}]

I'm trying to deserialize it with Restsharp's deserializer but when I print out the list, it shows that the properties are empty.

Here's how my code looks like:

    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);

Result when I print it on output:

   Beneficiary: 
   CurrencyCode: 
   Total: 0
   Beneficiary: 
   CurrencyCode: 
   Total: 0

EDIT: this is what the PaymentSummary class looks like:

    public class PaymentSummary
       {
           public string Beneficiary;
           public decimal Total;
           public string CurrencyCode;
       }

Your class is currently composed of public fields. RestSharp does not deserialize to fields, only public properties. You need to update it to the following:

public class PaymentSummary
{
    public string Beneficiary { get; set; }
    public decimal Total { get; set; }
    public string CurrencyCode { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM