Search code examples
c#jsondeserializationrestsharp

Deserialize RESTSharp Response Ignoring Headers


I am looking to deserialize the data returned from a REST API. I am struggling to get the data to map to my object. It seems like I am probably looking at the entire JSON response and not isolating in on the 'data' portion of the response. My response contains attributes for page, pagesize, hasmore and totalcount which I wish to ignore in my mapping. I want to map the array of data with only the fields that correspond to my object (there will be some additional fields in the response I do not need).

JSON Object

Here is the code that fails:

var businessUnits = JsonConvert.DeserializeObject<List<BusinessUnit>>(response.Content);

I'm really not sure what I am doing wrong. I've tried a dozen or so variations with absolutely no success. I can see the data I need in the data array; but any attempts to map it have failed.


Solution

  • you have to parse a json at first, after this you can select what part of the content you want to deserialize

    var businessUnits = JObject.Parse(response.Content)["data"]
                               .ToObject<List<BusinessUnit>>();