0

I am creating a nested custom type with primitive datatypes. I am having a Web APi that returns the data in JSON. using json2csharp.com, I am generating classes for the same.

I have decorated the primitive datatypes in all classes with DataMember and the types with DataContract.

I am using the following code for deserialization:

var resp = httpClient.GetAsync("http://ACTUAL_API_URI").Result;
var res = await resp.Content.ReadAsStringAsync();
var serializer = new DataContractJsonSerializer(typeof(RootObject));
byte[] byteArr= Encoding.ASCII.GetBytes(res);
var ms = new MemoryStream(byteArr);
var deserializedObj= (RootObject)serializer.ReadObject(ms);

I am not getting any exception. but the deserializedObj has null values for all the properties.

Any suggestions ?

dbc
  • 104,963
  • 20
  • 228
  • 340
Karthik
  • 79
  • 8
  • Can you provide api url? I guess problem in your `RootObject` class – Andrii Krupka Feb 16 '16 at 08:04
  • Please find the Types used: [DataContract] public class MyNestedType1 { [DataMember] public int Id { get; set; } [DataMember] public string BD { get; set; }[DataMember] public string UD { get; set; } } [DataContract] public class MyNestedType2 { [DataMember] public string Date { get; set; }[DataMember] public string U1 { get; set; } [DataMember] public string D1 { get; set; } } [DataContract] public class RootObject{[DataMember] public int Value { get; set; }[DataMember] public MyNestedType1 ObjectType1 { get; set; }[DataMember] public MyNestedType2 ObjectType2 { get; set; } } – Karthik Feb 16 '16 at 10:25
  • Also, Created a simple service and a contract as below: – Karthik Feb 16 '16 at 13:42
  • [DataContract] public class NestedType1{ [DataMember] public int ID { get; set; } [DataMember] public string BD { get; set; } [DataMember] public string UD { get; set; }}[DataContract] public class NestedType2 {[DataMember] public string Date { get; set; } [DataMember] public string S1 { get; set; } [DataMember] public string S2 { get; set; }}[DataContract] public class SampleData{ [DataMember] public int Value { get;set; } [DataMember] public NestedType1 NestedTypeObject1 { get;set; }[DataMember] public NestedType2 NestedTypeObject2 { get; set;}} – Karthik Feb 16 '16 at 13:49
  • Service as below: public IEnumerable Get() { return new SampleData[] { new SampleData() { Value=100, NestedTypeObject1 = new NestedType1 () { ID=101, BD="Description#1", UD="Description#2" }, NestedTypeObject2 = new NestedType2 () { Date=DateTime.Now.ToString(), S1="S1 String", S2="S2 String" } } }; } – Karthik Feb 16 '16 at 13:50
  • can you give me a json data? you can insert to `pastebin` and give me url – Andrii Krupka Feb 16 '16 at 13:51
  • Data returned from Service is as below: [{"Value":100,"NestedTypeObject1":{"ID":101,"BD":"Description#1","UD":"Description#2"},"NestedTypeObject2":{"Date":"2/16/2016 7:07:01 PM","S1":"S1 String","S2":"S2 String"}}] – Karthik Feb 16 '16 at 13:52
  • var httpClient = new HttpClient(); var resp = httpClient.GetAsync("http://localhost/MySampleService/api/Values/").Result; var res = await resp.Content.ReadAsStringAsync(); var serializer = new DataContractJsonSerializer(typeof(SampleData)); byte[] byteData = Encoding.ASCII.GetBytes(res); var ms = new MemoryStream(byteData); var deserializedObject = (SampleData)serializer.ReadObject(ms); – Karthik Feb 16 '16 at 13:53
  • JSON in pastebin http://paste.ubuntu.com/15091333/ – Karthik Feb 16 '16 at 13:58

1 Answers1

0

You have a couple mistakes.

  1. You returns collection of elements and try to deserialize one element instead collection

    public IEnumerable<SampleData> Get()
    {
        return new SampleData[]
        {
            new SampleData()
            {
                Value = 100,
                NestedTypeObject1 = new NestedType1()
                {
                    ID = 101,
                    BD = "Description#1",
                    UD = "Description#2"
                },
                NestedTypeObject2 = new NestedType2()
                {
                    Date = DateTime.Now.ToString(),
                    S1 = "S1 String",
                    S2 = "S2 String"
                }
            }
        };
    }
    

    so you just change your code to

    var serializer = new DataContractJsonSerializer(typeof(List<RootObject>));
    var deserializedObj= (List<RootObject>)serializer.ReadObject(ms);
    
  2. You have different model names between service side and client side, just you your SampleData model and all will be good. You must to rename MyNestedType1 to NestedTypeObject1 or add name to DataContractAttribute, e.g:

    [DataContract(Name = "NestedTypeObject1")]
    public class MyNestedType1
    {
        [DataMember]
        public int Id { get; set; }
        [DataMember]
        public string BD { get; set; }
        [DataMember]
        public string UD { get; set; }
    }
    

    It belongs for property names too.

Andrii Krupka
  • 4,276
  • 3
  • 20
  • 41
  • Thanks. I think the return type of IEnumerable in the Get method of WebAPi was the crux. As it returns a collection, while deserializing "the collection of datatype" has to be used. With this change, it was able to get the values. Property Name decoration was not required. It worked without it. – Karthik Feb 17 '16 at 08:40