RestSharp not deserializing JSON Object List, always Null RestSharp not deserializing JSON Object List, always Null asp.net asp.net

RestSharp not deserializing JSON Object List, always Null


Based on the @agarcian's suggestion above, I googled the error:

restsharp Data at the root level is invalid. Line 1, position 1.

and found this forum: http://groups.google.com/group/restsharp/browse_thread/thread/ff28ddd9cd3dde4b

Basically, I was wrong to assume that client.Execute was going to be able to auto-detect the return content type. It needs to be explicity set:

var request = new RestRequest(Method.GET);request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };

This could be cited more clearly in RestSharp's documentation. Hopefully this will help someone else out!


Late to the party: You would need to find the actual Content-Type of the response you were getting. The server doesn't necessarily respond with any of the content types from your request's Accept header. For Google's APIs I got a text/plain response, so this advice from the group worked for me.

public T Execute<T>(string url, RestRequest request) where T : new(){    var client = new RestClient();    // tell RestSharp to decode JSON for APIs that return "Content-Type: text/plain"    client.AddHandler("text/plain", new JsonDeserializer());    ...

It's also tidier if it can be done in one place such as the shared Execute method above, rather than forcing the response type with OnBeforeDeserialization wherever each request is created.


I had this same problem, and none of the answers above helped.

Here was my response class:

public class SomeResponse{    public string string1;    public string string2;}

The problem was human error - I forgot to include the get/set accessors. I don't often use C# and forgot the need for them.

public class SomeResponse{    public string string1 { get; set; }    public string string2 { get; set; }}

Hope this helps someone out there.