简体   繁体   English

将JSON反序列化为不同的类

[英]Deserialize JSON to different classes

I have an HttpClient that receives a json string from a REST Api. 我有一个HttpClient从REST Api接收一个json字符串。 Depending on being successful or not, different json structures are returned. 根据成功与否,返回不同的json结构。 I am using JSON.Net libraries to deserialize the string to different class and the code is throwing error. 我使用JSON.Net库将字符串反序列化为不同的类,代码抛出错误。 Here's my code 这是我的代码

If its successful, the json string would be: {"token":"9416285736761111","expiry":"1230","name":"ETS TEST CARD VISA"} 如果成功,json字符串将是:{“token”:“9416285736761111”,“expiry”:“1230”,“name”:“ETS TEST CARD VISA”}

if there's any error: {"errorCode":"2","errorMessage":"Invalid token"} 如果有任何错误:{“errorCode”:“2”,“errorMessage”:“无效令牌”}

My Classes are ReadCardResponse: 我的类是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) 返回的错误是:ReadCardResponse JSON:{“token”:“9416285736761111”,“expiry”:“1230”,“name”:“ETS TEST CARD VISA”}在CallSite.Target解析cc响应时出错(Closure,CallSite,Object)在System.Dynamic.UpdateDelegates.UpdateAndExecute1 [T0,TRet](CallSite站点,T0 arg0)

How do I go about deserializing a json to different classes? 我如何将json反序列化为不同的类?

You could read the status code from the response object of your HttpClient, and then if it's a 200 parse your normal object: 您可以从HttpClient的响应对象中读取状态代码,然后如果它是一个200解析您的普通对象:

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

and if it's a 4xx or 5xx code parse error object: 如果是4xx或5xx代码解析错误对象:

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

This does depend on the API you are using implementing status codes properly. 这取决于您正在使用的API正确实现状态代码。 Check out this post to get the status code: 查看此帖子以获取状态代码:

How to determine a 404 response status when using the HttpClient.GetAsync() 如何在使用HttpClient.GetAsync()时确定404响应状态

Never mind, figured it out. 没关系,想通了。 Solution is to access deserialized values as 解决方案是访问反序列化的值

_readCardResponse.expiry = _ccResponse["expiry"];
_readCardResponse.name = _ccResponse["name"];
_readCardResponse.token = _ccResponse["token"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM