繁体   English   中英

如何使用 c# 反序列化复杂的 JSON 对象?

[英]How to Deserialize complex JSON object using c#?

如何使用 c# 反序列化复杂的 JSON 对象,请在下面对 JSON 字符串进行反序列化? 我试图将事务属性作为字典而不工作,所以请知道吗?

将事务从数组替换为字典时出现异常:无法序列化 System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089] 类型的成员 wsA2AEBS.WCTransactionsResponse1.transactions ,[wsA2AEBS.WCTransInfoRes, wsA2AEBS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]],因为它实现了 IDictionary。 说明:在执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.NotSupportedException:无法序列化 System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], 类型的成员 wsA2AEBS.WCTransactionsResponse1.transactions wsA2AEBS.WCTransInfoRes, wsA2AEBS, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]],因为它实现了 IDictionary。

---------- JSON 文件

{
            ""applicationId"": ""ONB"",
            ""tranDateTime"": ""300817114023"",
            ""UUID"": ""e8289a8e-b522-4c56-b6df-d15b8cecb5d6"",
            ""responseStatus"": ""Successful"",
            ""responseCode"": 1,
            ""responseMessage"": ""Approved"",
            ""consumerIdentifier"": ""249915513503"",
            ""isConsumer"": true,
            ""fee"": 0.0,
            ""transactions"": {
                ""750271185417"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""750724170249"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""752569664009"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""750774501897"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""752401891849"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""753475633673"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_BALANCE"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""702396401949"": {
                    ""amount"": ""1.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": ""249915513503"",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0"",
                    ""type"": ""CASH_IN"",
                    ""sourceIdentifier"": ""313827219935"",
                    ""transactionName"": """"
                },
                ""751328150025"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""753391747593"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_TRANSACTIONS"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                },
                ""750422180361"": {
                    ""amount"": ""0.00"",
                    ""fee"": ""0"",
                    ""customerPayeeId"": """",
                    ""destinationIdentifier"": """",
                    ""status"": ""COMPLETED"",
                    ""payeeId"": """",
                    ""billId"": """",
                    ""externalFee"": ""0.00"",
                    ""type"": ""GET_BALANCE"",
                    ""sourceIdentifier"": ""300136232415"",
                    ""transactionName"": """"
                }
            },
            ""transactionId"": ""754046059017""
        }

---------- 类

public class WCTransactionsResponse
    {
        public string applicationId;
        public string tranDateTime;
        public string UUID;
        public string consumerIdentifier;
        public bool isConsumer;
        public string extraData;
        //public string notifyConsumer;
        //public string tranFeeAmount;
        //public string feeSource;
        //public string feeShare;
        public string responseStatus;
        public string responseCode;
        public string responseMessage;
        public string fee;
        public WCTransInfoRes[] transactions;
        public string transactionId;
    }



public class WCTransInfoRes
    {
        public string status;
        public string type;
        public string amount;
        public string fee;
        public string externalFee;
        public string sourceIdentifier;
        public string destinationIdentifier;
        public string billId;
        public string payeeId;
        public string customerPayeeId;
        public string transactionName;
    }

---------- 转换 Json 字符串

 WCTransactionsResponse Resp = new WCTransactionsResponse();

            var json = File.ReadAllText(@"E:\wsA2AEBS\wsA2AEBS\json.json");
            //var jObject = JObject.Parse(json);

            Resp = JsonConvert.DeserializeObject<WCTransactionsResponse>(json);

            return Resp;// jObject.ToString();

你说它是一本字典,但你把它声明为数组。

将定义更改为Dictionary

public Dictionary<string, WCTransInfoRes> transactions;

暂无
暂无

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

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