简体   繁体   中英

How do I parse a RestSharp response into a class?

I'm using RestSharp to handle sending emails and I need to be able to check the response to make sure everything went okay.

I know absolutely nothing about JSON but what I've seen from my searching has lead me to believe that calling client.Execute<T>( Foo ) should result in me getting an object of type T with properties populated by the result of the request execution...

This is not the case.

In the case where I intentionally fail the POST I get this from the response.Content property :

{"error":3,"message":"Wrong credentials specified"}

When I submit the request with the proper credentials, response.Content looks like this :

{"message":"OK"}

which lead me to believe creating a class like this should be all that I would need :

public class RestMessage {
    string error { get; set; }
    string message { get; set; }
}

but when I call

IRestResponse<RestMessage> Foo = Bar.Execute<RestMessage>( Baz );

Foo.Data equals an object of type RestMessage , but in either case the result is always that error = null ( which makes sense in the latter case, but not in the former ) and message = null .

Clearly it is not as simple as every example I have read makes it out to be.

I have installed RestSharp from NuGet in VS2015 Community using their awesome NuGet... thingy so I have the most recent version.

Can someone explain how I can accomplish what I am trying to do to me as if I were still teething?

Thanks to Gusman for pointing this out.

The answer was so simple I hate myself for not seeing it, and there is now a face-shaped indentation in my desk, and a desk-shaped indentation where my face used to be...

Before :

public class RestMessage{
    string error{ get; set; }
    string message{ get; set; }
}

After :

public class RestMessage{
    public string error{ get; set; }
    public string message{ get; set; }
}

Don't drink and code kids...

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