У меня есть HttpClient, который получает строку json от REST Api. В зависимости от того, успешны они или нет, возвращаются разные структуры json. Я использую библиотеки JSON.Net для десериализации строки в другой класс, и код выдает ошибку. Вот мой код
В случае успеха строка json будет:
{"token": "9416285736761111", "expiry": "1230", "name": "ETS TEST CARD VISA"}
если есть ошибки:
{"errorCode": "2", "errorMessage": "Неверный токен"}
Мои классы
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";
}
Ошибка возвращается:
ReadCardResponse JSON: {"token": "9416285736761111", "expiry": "1230", "name": "ETS TEST CARD VISA"}
Ошибка разбора ответа cc
в CallSite.Target (Закрытие, CallSite, Объект)
в System.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0, TRet] (сайт CallSite, T0 arg0)
Как мне десериализовать JSON в разные классы?