簡體   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