简体   繁体   中英

RestSharp: Converting results

I get the following JSON that I am trying to convert to a business object using RestSharp

  {
  "valid":true,
  "data":[
    {
       "dealerId":"4373",
       "branchId":"4373",
    }
  ]
  }

I wish to convert to:

public class Dealer
{
    public string dealerId ;
    public string branchId;
   }

But this fails, though the JSON is fine:

        var client = new RestClient("http://www.????.com.au");
        var request = new RestRequest(string.Format("service/autocomplete/dealer/{0}/{1}.json", suburb.PostCode, suburb.City.Trim().Replace(" ", "%20")), Method.GET);
        var response2 = client.Execute<Dealer>(request);
        return response2.Data;

Your business object doesn't match the response JSON you are getting back. If you want your response to serialize, your C# object would look something like

public class DealerResponse
{
    public bool valid { get;set; }
    List<Dealer> data { get;set; }
}

public class Dealer
{
    public string dealerId;         
    public string branchId;   
}

I haven't tested this code, but even though you are only interested in the information in 'data', your response C# objects still need to represent the whole JSON response to serialize correctly.

Hope that helps.

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