iam.Carrot
iam.Carrot

Reputation: 5286

Deserialization in Json data returns null values

I am working in a windows 10 Universal app, while trying to receive data from my webAPI, my code is as follows:

        try
        {
            string _serviceUrl = Constants.BaseUrl + "api/RegisterBindingModels?email=" + Useremail;

            HttpClient client = new HttpClient();

            HttpResponseMessage responce = await client.GetAsync(new Uri(_serviceUrl));

            if (responce.Content != null)
            {
                var obj = await responce.Content.ReadAsStringAsync();


                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.MissingMemberHandling = MissingMemberHandling.Ignore;
                var rcvdData = JsonConvert.DeserializeObject<RegisterModel>(obj, settings);
            }
        }
        catch (Exception)
        {

            throw;
        }

while this code runs, the Obj gets the following JSON, which is as expected:

{
   "UserDetails":{
      "UserId":1,
      "FullName":"sample string 2",
      "Username":"sample string 3",
      "ICEFullName":"sample string 4",
      "ICEMobileNumber":5,
      "DoctorFullName":"sample string 6",
      "DoctorMobileNumber":7
   },
   "UserId":1,
   "Email":"[email protected]",
   "Password":"add‌​sFABBS!2",
   "ConfirmPassword":"addsFABBS!2"
}

Nevertheless the var rcvdData has all null values:

like this

The RegisterModel is as follows:

namespace APIValueSetterTest.Model
{
    using System.Runtime.Serialization;

    [DataContract]
    public class RegisterModel
    {
        public int UserId { get; set; }

        public string Email { get; set; }

        public string Password { get; set; }

        public string ConfirmPassword { get; set; }

        public UserDetails UserDetails { get; set; }
    }
}

and the UserDetails Model is as follows:

namespace APIValueSetterTest.Model
{
    using System.Runtime.Serialization;

    [DataContract]
    public class UserDetails
    {
        public int UserId { get; set; }

        public string FullName { get; set; }

        public string Username { get; set; }

        public string ICEFullName { get; set; }

        public int ICEMobileNumber { get; set; }

        public string DoctorFullName { get; set; }

        public int DoctorMobileNumber { get; set; }
    }
}

I need help, where am I going wrong?

Upvotes: 0

Views: 70

Answers (1)

dbc
dbc

Reputation: 117001

The problem is that you have marked your types with [DataContract] but you have not marked each member to be serialized with [DataMember]. Explicit data contract serialization is opt-in, as is explained in Using Data Contracts:

You can also explicitly create a data contract by using DataContractAttribute and DataMemberAttribute attributes. This is normally done by applying the DataContractAttribute attribute to the type. This attribute can be applied to classes, structures, and enumerations. The DataMemberAttribute attribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized. For more information, see Serializable Types.

As supports data contracts, as explained in its docs, you need to remove [DataContract], or add [DataMember] to all serializable members.

Upvotes: 1

Related Questions