0

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?

SoftwareDveloper
  • 559
  • 1
  • 5
  • 18

2 Answers2

0

You could read the status code from the response object of your HttpClient, and then if it's a 200 parse your normal object:

dynamic_ccResponse = JsonConvert.DeserializeObject<ReadCardResponse>(ccResultJson);

and if it's a 4xx or 5xx code parse error object:

dynamic_ccResponse = JsonConvert.DeserializeObject<ErrorResponse>(ccResultJson);

This does depend on the API you are using implementing status codes properly. Check out this post to get the status code:

How to determine a 404 response status when using the HttpClient.GetAsync()

Ben Krueger
  • 1,476
  • 1
  • 14
  • 20
0

Never mind, figured it out. Solution is to access deserialized values as

_readCardResponse.expiry = _ccResponse["expiry"];
_readCardResponse.name = _ccResponse["name"];
_readCardResponse.token = _ccResponse["token"];
SoftwareDveloper
  • 559
  • 1
  • 5
  • 18