简体   繁体   中英

Nested JSON Object deserialization in C# (WCF) service

I have a written JsonHelper class with two methods Serialized and Deserilized. when I tried with Master/Detail model with 2-3 items. It works fine and convert the deserilized object but when I tried with my real data which I will be receiving then I am receiving detail collection (nested object) always null. I have created Unit test for my service which works fine because I am passing the Object with collections through C# code. But when I received Android post in a Json string. It returns me nested object OrderItemDetail always null. I tried everything but no luck.

Here is my Model Master class

 [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; }
    }

Here is my OrderItemDetail class which can be multiple

  [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; }
    }

Json string which I received from Android App, I modified it for C# format.

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\"" +
                       "}]" +
                       "}";

Here is the call

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

Which returns always OrderItemDetail as null.

Here is my JsonHelper class

 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;
        }
    }

I have spent more than 5 hours to this problem, Please help me fix this what I am missing on it.

I generated the classes with json2csharp.com and difference is only in datatypes

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; }
}

Could you try it ?

I didnt found any other problem

if it still dont work could you post value of variable jss ?

I have read your comments, but I think the solution you found misses the real issue. If you remove both [DataContract] and [DataMember] tags, it just on default will serialize all public fields and properties. In your original code listing you needed to have the [DataMember] tag on the Collection<OrderItemDetail> object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM