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

问题描述 投票:-2回答:1

如何使用c#反序列化复杂的JSON对象,我需要在下面的DeSerialize 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 String

 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();
c# .net json deserialization jsonconverter
1个回答
0
投票

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

将定义更改为Dictionary

public Dictionary<string, WCTransInfoRes> transactions;
© www.soinside.com 2019 - 2024. All rights reserved.