Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I’m attempting to deserialize the following Json string to c# .net class list collection, but the Jsonconvert.deserialize returns null. Can someone please explain to me what I’m doing wrong.

string strjson = "{ 'TestInfo':[{'testId':1,'testShortDescription':'TankTest','testLongDescription':'Tank Test ','testTypeId':2,'testLimitsId':null},{'testId':2,'testShortDescription':'FuelPump1','testLongDescription':'Fuel Pump 1','testTypeId':4,'testLimitsId':null},{'testId':3,'testShortDescription':'FuelPump2','testLongDescription':'Fuel Pump 2','testTypeId':4,'testLimitsId':null},{'testId':4,'testShortDescription':'TankAudit','testLongDescription':'Tank Chamber Audit','testTypeId':6,'testLimitsId':null}]}";
                      TestInfoCollectionCS testinfocollection = JsonConvert.DeserializeObject<TestInfoCollectionCS>(strjson);


Here is the collection class:

using System;
using System.Collections.Generic;
using System.Text;
using TestITMobileApp.Model;

namespace TestITMobileApp.App_Data
{
    public class TestInfoCollectionCS
    {
        private List<TestInfoCS> testinfocollection;

        public List<TestInfoCS> TestInfoCollection { get => testinfocollection; set => testinfocollection = value; }
    }
}


Here is the call:

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace TestITMobileApp.Model
{
    public class TestInfoCS 
    {
        #region "Private variables""
        private int testId;
        private string testShortDescription;
        private string testLongDescription;
        private int? testTypeId;
        private int? testLimitsId;
        #endregion
        #region "Properties"
        #region "TestID"
        public int TestID { get => testId; set => testId = value; }
        #endregion
        #region "TestShortDescription"
        public string TestShortDescription { get => testShortDescription; set => testShortDescription = value; }
        #endregion
        #region "TestLongDescription"
        public string TestLongDescription { get => testLongDescription; set => testLongDescription = value; }
        #endregion
        #region "TestTypeId"
        public int? TestTypeId  { get => testTypeId; set => testTypeId = value; }
        #endregion
        #region "TestLimitsId"
        public int? TestLimitsId { get => testLimitsId; set => testLimitsId = value; }
        #endregion
        #endregion
    }
}


What I have tried:

Tried running the above code, but json convert deserialization returns null.
Posted
Updated 7-Oct-19 4:37am

The "case" doesn't match.

The JSON element names are lower case; the class properties are upper case (the fields are lower case and not accessible).
 
Share this answer
 
v2
Comments
vinodambadi 23-Aug-23 7:22am    
Thanks this helped
TestInfo node in the JSON does not match to the property TestInfoCollection.
JsonConvert.DeserializeObject can leave reference type member properties null during deserialization without [JsonProperty] attribute on the property.

Try this:

1. Synchronize the property name in the class and in the JSON file.
2. Add [JsonProperty] attribute to the property in the C# class.

Or add attribute [JsonProperty("TestInfo")] to the TestInfoCollection property.

// Both of these approaches helped me in the same situation.
 
Share this answer
 
What I have done in the past, where the serializer is returning null on Deserialize, is to test Serializing my object to see what the serializer is expecting. Once I see that result, I can fix my objects so they now Deserialize correctly.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900