RestSharp Deserialization with JSON Array

Garrett Vlieger picture Garrett Vlieger · Apr 22, 2013 · Viewed 27.4k times · Source

I have a JSON response that I'm trying to deserialize with RestSharp, and it looks like this:

{"devices":[{"device":{"id":7,"deviceid":"abc123","name":"Name"}},
            {"device":{"id":1,"deviceid":"def456","name":"Name"}}],
 "total":2,
 "start":0,
 "count":2}

Based off of some suggestions I've found, I've tried to setup my POCO like this:

public class DevicesList
{
    public List<DeviceContainer> Devices;
}

public class DeviceContainer
{
    public Device Device;
}

public class Device
{
    public int Id { get; set; }
    public string DeviceId { get; set; }
    public string Name { get; set; }
}

And then my execution looks like this:

// execute the request
var response = client.Execute<DevicesList>(request);

However, response.Data is NULL, and I've tried other variations with no luck.

So, what class structure and mapping should be used for this situation? I've also tried this without the extra DeviceContainer class.

Thanks for the help.

Answer

Boycs picture Boycs · May 28, 2013

I had a slightly different issue when my deserialization POCO contained an array..

Changing it from Devices[] to List<Devices> resolved the issue and it deserialized correctly.