deserialize Json using JavaScriptSerializer Custom Types

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-05-13T17:51:45Z Indexed on 2010/05/13 18:04 UTC
Read the original article Hit count: 463

Filed under:
|
|
|

I have a RESTFUL WCF service returning a .NET custom type as json string. I am using .NET 3.5 framework and I am using JavaScriptSerializer for deserializing it in to my custom type. Serialization to json is handled by WCF.

using (WebResponse resp = req.GetResponse())
            {
                using (System.IO.StreamReader sreader = new System.IO.StreamReader(resp.GetResponseStream()))
                {
                    string jsonString = sreader.ReadToEnd();
                    CustomType myType = new CustomType();
                    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    myType = serializer.Deserialize<CustomType>(jsonString);
                    return myType;
                }
            }

The problem is that, I have a property (or element) of the CustomType which is generic list of another custom type ChildObject. They are in different namespaces. When I deserialize using the code above, I get all the properties of the CustomType myType including a list of ChildObject. But all the properties of the ChildObject is null. The string returned from the stream reader has all the values for the ChildObject. the values are lost when deserializing. Can anyone help me with this please?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about serialization