如何使用 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:无法序列化成员 wsA2AEBS.WCTransactionsResponse1.transactions 类型 System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[ 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();