如何使用C#反序列化复杂的JSON对象,请我需要 在下面反序列化JSON字符串?我试图把 交易属性作为字典,不起作用,因此请 想法?
从数组到字典的替换事务时发生异常:无法序列化成员wsA2AEBS.WCTransactionsResponse1.transactions 类型System.Collections.Generic.Dictionary`2 [[System.String, mscorlib,版本= 4.0.0.0,文化=中性, PublicKeyToken = b77a5c561934e089],[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, 版本= 4.0.0.0,文化=中性, 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();
答案 0 :(得分:0)
您说这是一个字典,但您将其声明为数组。
将定义更改为Dictionary
:
public Dictionary<string, WCTransInfoRes> transactions;