I have an HttpClient that receives a json string from a REST Api. Depending on being successful or not, different json structures are returned. I am using JSON.Net libraries to deserialize the string to different class and the code is throwing error. Here's my code
If its successful, the json string would be: {"token":"9416285736761111","expiry":"1230","name":"ETS TEST CARD VISA"}
if there's any error: {"errorCode":"2","errorMessage":"Invalid token"}
My Classes are ReadCardResponse:
public class ReadCardResponse
{
public string token{get;set;}
public string expiry {get; set;}
public string name {get;set;}
public string merchid { get; set; }
public int amount { get; set; }
public string tokenize { get; set; }
public string orderId { get; set; }
}
ErrorResponse:
public class ErrorResponse
{
public string errorCode{get;set;}
public string errorMessage{get;set;}
}
dynamic_ccResponse = JsonConvert.DeserializeObject(ccResultJson);
if ((_ccResponse.token.Length > 5) && (_ccResponse.expiry.Length >= 3))
{
_readCardResponse = new ReadCardResponse();
_replyCode = "1";
_readCardResponse.expiry = _ccResponse.expiry;
_readCardResponpse.name = _ccResponse.name;
_readCardResponse.token = _ccResponse.token;
//Use the below notation to access values
readCardResponse.expiry = _ccResponse["expiry"];
_readCardResponse.name = _ccResponse["name"];
_readCardResponse.token = _ccResponse["token"];
_readCardResponse.amount = _requestObject.amount;
_readCardResponse.orderId = _requestObject.orderId;
_readCardResponse.tokenize = "y";
}
else if (Convert.ToInt32(_ccResponse.errorCode) == 1) //timeout
{
_replyCode = "2";
}
else if (Convert.ToInt32(_ccResponse.errorCode) == 8) //cancel button was pressed on the terminal
{
_replyCode = "8";
}
Error returned is: ReadCardResponse JSON: {"token":"9416285736761111","expiry":"1230","name":"ETS TEST CARD VISA"} Error parsing cc response at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
How do I go about deserializing a json to different classes?