0

我正在尝试反序列化一个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; }
}
4

1 回答 1

0

您的代码的问题在于,在您的 JSON 中,对象中OrderData成员的类型CustomerData是一个数组,但在您的类型定义中它是一个对象。同样,SourceData您的 JSON 中的 The 是一个数组,但在您的类型定义中是一个对象。

如果您如下所示更改类型定义,您应该能够使用DataContractJsonSerializer.

public class StackOverflow_15752476
{
    const string jsonString = @" 
        {
            ""RequestId"":514106,
            ""Warning"":[],
            ""CustomerData"": {
                ""Email"":""abc@abc.com"",
                ""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; }
    }
}
于 2013-04-01T23:12:48.737 回答