0
我試圖反序列化一個Json
字符串。我的反序列化操作可以達到OrderData
。 當我反序列化OrderData
總是null
和SourceData
也沒有顯示出來。它也不會引發任何錯誤。任何建議來解決這個問題?DataContractJsonSerializer去反序列化Json
string jsonString = @"
{
""RequestId"":514106,
""Warning"":[],
""CustomerData"": {
""Email"":""[email protected]"",
""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; }
}