20

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.

1
  • May be you should add total, start and count properties to deviceslist class. Commented Apr 22, 2013 at 21:14

6 Answers 6

34

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, just got hit with this and your fix solved it
IEnumerable<> and IList<> don't work either. You need List<>
20

RestSharp only operates on properties, it does not deserialize to fields, so make sure to convert your Devices and Device fields to properties.

Also, double check the Content-Type of the response, if the responses is something non-default, RestSharp may not uses the JsonDeserializer at all. See my answer on RestSharp client returns all properties as null when deserializing JSON response

1 Comment

That was it. Stupid mistake forgetting the { get; set; }. Once I added that, it worked perfectly. Thanks!
5

Something that I ran into is, it does not work if your using interfaces like: IEnumerable or IList, it has to be a concrete type.

This will not work, where as it does for some other json serializers like json.net.

public class DevicesList
{
    public IEnumerable<DeviceContainer> Devices { get; set; }
}

public class DeviceContainer
{
   ...
}

it would have to be something like this:

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

public class DeviceContainer
{
   ...
}

1 Comment

This helps if you are using a newer version of RESTSharp (e.g. not using JSON.NET) for your JsonDeserializer
2

RestShartp doesn't support DataAnnotation/DataMember, rename your properties with no maj:

  • Devices -> devices
  • Device -> device

AND don't forget the {get; set;} ;).

Comments

0

My problem was entirely different, I naively thought JsonDeserializer supports JsonProperty attribute, but thats not true. So when trying to deserialize into

public class AvailableUserDatasApi
{
    [JsonProperty("available-user-data")]
    public List<AvailableUserDataApi> AvailableUserDatas { get; set; }
}

it failed.. But changing AvailableUserDatas to AvailableUserData was enough for things to start working.

Comments

0

In .Net Core you can use native attribute (since Newtonsoft is deprecated):

public class AvailableUserDatasApi
{
    [JsonPropertyName("available-user-data")]
    public List<AvailableUserDataApi> AvailableUserDatas { get; set; }
}

Then it is possible to use any property name you want.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.