C#(WCF)服务中的嵌套JSON对象反序列化

时间:2015-12-17 12:45:03

标签: c# json wcf serialization

我有一个带有两个方法Serialized和Deserilized的JsonHelper类。当我尝试使用2-3个项目的Master / Detail模型时。它工作正常并转换deserilized对象,但当我尝试我将接收的真实数据然后我收到详细信息集合(嵌套对象)始终为null。 我已经为我的服务创建了单元测试,因为我通过C#代码传递带有集合的Object。但是当我在Json字符串中收到Android帖子时。它返回我嵌套对象OrderItemDetail总是为null。我尝试了一切,但没有运气。

这是我的Model Master类

 [DataContract]

    public class InitialOrder
    {
        [DataMember]
        public int? ResturantId { get; set; }
        [DataMember]
        public int? EmpId { get; set; }
        [DataMember]
        public int? TableId { get; set; }
        [DataMember]
        public int? StatusId { get; set; }
        [DataMember]
        public bool? IsTakeAway { get; set; }
        [DataMember]
        public int? NoOfPersons { get; set; }
        public Collection<OrderItemDetail> OrderItemDetails { get; set; }
    }

这是我的OrderItemDetail类,它可以是多个

  [DataContract]
   public class OrderItemDetail
    {
        [DataMember]
        public int ItemId { get; set; }
        [DataMember]
        public decimal? UnitPrice { get; set; }
        [DataMember]
        public int? Quantity { get; set; }
        [DataMember]
        public decimal? TotalPrice { get; set; }
        [DataMember]
        public decimal? DiscountAmount { get; set; }
        [DataMember]
        public decimal? DiscountPerc { get; set; }
        [DataMember]
        public string Notes { get; set; }
    }

我从Android App收到的Json字符串,我将其修改为C#格式。

string jss = "{" +
                       "\"EmpId\": 1," +
                       "\"IsTakeAway\": true," +
                       "\"NoOfPersons\": 3," +
                       "\"ResturantId\": 1," +
                       "\"StatusId\": 2," +
                       "\"TableId\": 7," +
                       "\"OrderItemDetails\": [" +
                       "{" +
                            "\"ItemId\": 1," +
                            "\"Quantity\": 1," +
                            "\"UnitPrice\": 1.1," +
                            "\"DiscountAmount\": 1.0," +
                            "\"DiscountPerc\": 1.0," +
                            "\"TotalPrice\": 1.0," +
                            "\"Notes\": \"Test\"" +
                       "}," +
                       "{" +
                            "\"ItemId\": 1," +
                            "\"Quantity\": 1," +
                            "\"UnitPrice\": 1.1," +
                            "\"DiscountAmount\": 1.0," +
                            "\"DiscountPerc\": 1.0," +
                            "\"TotalPrice\": 1.0," +
                            "\"Notes\": \"Test\"" +
                       "}]" +
                       "}";

这是电话

InitialOrder initialOrder= JsonHelper.JsonDeserialize<InitialOrder>(jss);

它总是将OrderItemDetail返回为null。

这是我的JsonHelper类

 public class JsonHelper
    {
        public static string JsonSerializer<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, t);
            string jsonString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return jsonString;
        }
        /// <summary>
        /// JSON Deserialization
        /// </summary>
        public static T JsonDeserialize<T>(string jsonString)
        {
            T obj = Activator.CreateInstance<T>();
            MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            obj = (T)serializer.ReadObject(ms);
            ms.Close();
            return obj;
        }
    }

我花了5个多小时才解决这个问题,请帮我解决一下我所缺少的问题。

2 个答案:

答案 0 :(得分:0)

我使用json2csharp.com生成了类,而差异仅在于数据类型

public class OrderItemDetail
{
    public int ItemId { get; set; }
    public int Quantity { get; set; }
    public double UnitPrice { get; set; }
    public double DiscountAmount { get; set; }
    public double DiscountPerc { get; set; }
    public double TotalPrice { get; set; }
    public string Notes { get; set; }
}    

public class RootObject
{
    public int EmpId { get; set; }
    public bool IsTakeAway { get; set; }
    public int NoOfPersons { get; set; }
    public int ResturantId { get; set; }
    public int StatusId { get; set; }
    public int TableId { get; set; }
    public List<OrderItemDetail> OrderItemDetails { get; set; }
}

你能尝试吗?

我没有发现任何其他问题

如果它仍然不起作用,你可以发布变量jss的值吗?

答案 1 :(得分:0)

我已阅读您的评论,但我认为您找到的解决方案错过了真正的问题。如果同时删除[DataContract][DataMember]标记,则默认情况下会将所有公共字段和属性序列化。在原始代码清单中,您需要在[DataMember]对象上添加Collection<OrderItemDetail>标记。