none
JsonConvert.DeserializeObject<T>(JsonString) returning all Properties<T> as Null RRS feed

  • Question

  • Hi Every one

    I have a service which returning following json

    {
    
        "PatientLoginResult": 
    
        {
            "ContactNo": "9030149435",
            "Email": "asif@gmail.com",
            "Gender": "Male",
            "Id": 1,
            "Name": "Asif",
            "Password": "Test123!"
        }
    
    }

    I want to Deserialize into .net Object so i created following class

     [JsonObject(Id = "PatientLoginResult")]
        public class PatientDTO
        {
            [JsonProperty(PropertyName = "Id")]
            public int Id { get; set; }
            [JsonProperty(PropertyName = "Name")]
            public string Name { get; set; }
            [JsonProperty(PropertyName = "Password")]
            public string Password { get; set; }
            [JsonProperty(PropertyName = "Gender")]
            public string Gender { get; set; }
            [JsonProperty(PropertyName = "ContactNo")]
            public string ContactNo { get; set; }
            [JsonProperty(PropertyName = "Email")]
            public string Email { get; set; }
    
        }
    
     var patient = JsonConvert.DeserializeObject<PatientDTO>(result);

    where result is above mentioned result .

    But all property of patient object are null .

    I am not able to track the error . can anybody help me .

    thanx in advance




    Ramakant Verma .NET Consultant(Windows Azure, Windows Phone8,Windows 8,ASP.NET,Silverlight)

    Monday, June 8, 2015 5:58 AM

Answers

  • Hi Ramakant Verma,

    Welcome to the Developing Universal Windows apps forum! 
    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools .

    >>But all property of patient object are null .

    Based on your JSON, I see that you have the root name(class name) PatientLoginResult inside it, in that way we can not deserialize it into object directly by using your code: var patient = JsonConvert.DeserializeObject<PatientDTO>(result), if your JSON is something like the following string then your code should work fine:

    {
            "ContactNo": "9030149435",
            "Email": "asif@gmail.com",
            "Gender": "Male",
            "Id": 1,
            "Name": "Asif",
            "Password": "Test123!"
        }

    >>Is there any other to Deserialize it into object without using linq to json??

    If you do not want to use your solution to deserialize your JSON, I will recommend you define the PatientLoginResult in a new class, in this way we can deserialize it into object directly, for more information, please try to refer to the following code:

    public class TestClass
    {
        public PatientDTO PatientLoginResult { get; set; }
    }
    .....
    var patient = JsonConvert.DeserializeObject<TestClass>(result);
    Result:



    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 10, 2015 7:02 AM
    Owner

All replies

  • Finally i get the answer by using the linq to json using the following code

    IIs there any other to Deserialize it into object without using linq to json??

    var result =await response.Content.ReadAsStringAsync();
                JObject jobject = JObject.Parse(result);
                var patient =JsonConvert.DeserializeObject<PatientDTO>( jobject["PatientLoginResult"].ToString())


    Ramakant Verma .NET Consultant(Windows Azure, Windows Phone8,Windows 8,ASP.NET,Silverlight)

    Monday, June 8, 2015 6:50 AM
  • Hi Ramakant Verma,

    Welcome to the Developing Universal Windows apps forum! 
    Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools .

    >>But all property of patient object are null .

    Based on your JSON, I see that you have the root name(class name) PatientLoginResult inside it, in that way we can not deserialize it into object directly by using your code: var patient = JsonConvert.DeserializeObject<PatientDTO>(result), if your JSON is something like the following string then your code should work fine:

    {
            "ContactNo": "9030149435",
            "Email": "asif@gmail.com",
            "Gender": "Male",
            "Id": 1,
            "Name": "Asif",
            "Password": "Test123!"
        }

    >>Is there any other to Deserialize it into object without using linq to json??

    If you do not want to use your solution to deserialize your JSON, I will recommend you define the PatientLoginResult in a new class, in this way we can deserialize it into object directly, for more information, please try to refer to the following code:

    public class TestClass
    {
        public PatientDTO PatientLoginResult { get; set; }
    }
    .....
    var patient = JsonConvert.DeserializeObject<TestClass>(result);
    Result:



    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, June 10, 2015 7:02 AM
    Owner