0

我試圖反序列化一個Json字符串。我的反序列化操作可以達到OrderData。 當我反序列化OrderData總是nullSourceData也沒有顯示出來。它也不會引發任何錯誤。任何建議來解決這個問題?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; } 
} 

回答

0

的問題與您的代碼的datacontracts是,在你的JSON的OrderData成員的類型在CustomerData對象中是一個數組,但在你的類型定義中它是一個對象。同樣,你的JSON中的SourceData是一個數組,但是在你的類型定義中是一個對象。

如果您更改了如下所示的類型定義,您應該能夠使用DataContractJsonSerializer正確地反序列化它。

public class StackOverflow_15752476 
{ 
    const string jsonString = @" 
     { 
      ""RequestId"":514106, 
      ""Warning"":[], 
      ""CustomerData"": { 
       ""Email"":""[email protected]"", 
       ""FullName"":""OrTguOfE"", 
       ""OrderData"":[{ 
        ""OrderId"":""123"", 
        ""SourceId"":""0"", 
        ""SourceData"": [{ 
         ""SourceDescription"":""This is sourcedesc"", 
         ""ProductName"":""xyzabc"" 
        }] 
       }] 
      } 
     }"; 

    public static void Test() 
    { 
     RecordInfo records = Deserialize<RecordInfo>(jsonString); 
     Console.WriteLine(records.CustomerData.OrderData.Length); 
     Console.WriteLine(records.CustomerData.OrderData[0].OrderId); 
     Console.WriteLine(records.CustomerData.OrderData[0].SourceId); 
     Console.WriteLine(records.CustomerData.OrderData[0].SourceData[0].ProductName); 
    } 

    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; } 
    } 
} 
相關問題