I'm working on an API integration with JSON. I have not implemented anything yet in my C # code, I am currently studying the API but I have already had a question.
I have a login method that if successful returns me a type of response, and if it fails I get a totally different type.
Example:
If successful, the return is:
{
"Autenticacao": {
"tokenAutenticacao": "9f6530dad90c7f8eda5670"
"idUsuario": "c7f8eda5670"
}
}
Using json2charp I create the following class:
public class Autenticacao
{
public string tokenAutenticacao { get; set; }
public string idUsuario { get; set; }
}
public class LoginSucesso
{
public Autenticacao Autenticacao { get; set; }
}
And if login fails, the return is:
{
"status": {
"code": 403,
"name": "Forbidden"
},
"mensagem": "Login falhou",
"exception": "Blah Blah blah",
"dataHora": "18/06/2015 10:47:47"
}
Converting to a C # class I have:
public class Status
{
public int code { get; set; }
public string name { get; set; }
}
public class LoginFalha
{
public Status status { get; set; }
public string mensagem { get; set; }
public string exception { get; set; }
public string dataHora { get; set; }
}
Note: I've changed RootObject for LoginSuccess and LoginFail.
I want to use Json.NET to deserialize the JSON object, doing something like this:
LoginSucesso resultadoJson = JsonConvert.DeserializeObject<LoginSucesso>(meuJson);
But what if login fails?
How should I handle C # code?
I treat an exception and try LoginFalha resultadoJson = JsonConvert.DeserializeObject<LoginFalha>(meuJson);
????