我正在尝试反序列化一个Json
字符串。我的反序列化最高可达OrderData
. 当我反序列化时OrderData
总是null
并且SourceData
也没有出现。它也没有抛出任何错误。有什么建议可以解决这个问题吗?
string jsonString = @"
{
""RequestId"":514106,
""Warning"":[],
""CustomerData"": {
""Email"":""abc@abc.com"",
""FullName"":""OrTguOfE"",
""OrderData"":[{
""OrderId"":""123"",
""SourceId"":""0"",
""SourceData"": [{
""SourceDescription"":""This is sourcedesc"",
""ProductName"":""xyzabc""
}]
}]
}
}";
我的反序列化代码在这里:
RecordInfo records= Deserialize<RecordInfo>(jsonString);
private static T Deserialize<T>(string jsonString)
{
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
{
var serializer = new DataContractJsonSerializer(typeof(T));
return (T)serializer.ReadObject(ms);
}
}
这是我使用的数据合同
[DataContract]
public class RecordInfo
{
[DataMember(Name = "RequestId")]
public string RequestId { get; set; }
[DataMember(Name = "Warning")]
public string Warning { get; set; }
[DataMember(Name = "CustomerData")]
public CustomerData CustomerData { get; set; }
}
[DataContract]
public class CustomerData
{
[DataMember(Name = "Email")]
public string RequestId { get; set; }
[DataMember(Name = "FullName")]
public string FullName { get; set; }
[DataMember(Name = "OrderData")]
public OrderData OrderData { get; set; }
}
[DataContract]
public class OrderData
{
[DataMember(Name = "OrderId")]
public string OrderId { get; set; }
[DataMember(Name = "SourceId")]
public string SourceId { get; set; }
[DataMember(Name = "SourceData")]
public SourceData SourceData { get; set; }
}
[DataContract]
public class SourceData
{
[DataMember(Name = "SourceDescription")]
public string SourceDescription { get; set; }
[DataMember(Name = "ProductName")]
public string ProductName { get; set; }
}