Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Why does jsoncovert deserialization return null?
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 testinfocollections = JsonConvert.DeserializeObject<TestInfoCollectionCS>(strjson);

Here is the TestCS class:
using System;
using System.Collections.Generic;
using System.Text;
namespace TestITMobileApp.Models
{
    public class TestCS
    {
        #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
    }
}

The class TestInfoCollectionCS that contains the list class:
using System;
using System.Collections.Generic;
using System.Text;
using TestITMobileApp.Model;
using TestITMobileApp.Models;

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

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


What I have tried:

Deserialization returning null?
Posted
Updated 13-Feb-19 8:11am
Comments

1 solution

Probably, your class names don't match.
If I feed your JSON into a class generator (json2csharp - generate c# classes from json[^]) I get these:
public class Testinfo
    {
    public int testid { get; set; }
    public string testshortdescription { get; set; }
    public string testlongdescription { get; set; }
    public int testtypeid { get; set; }
    public object testlimitsid { get; set; }
    }

public class RootObject
    {
    public List<Testinfo> testinfo { get; set; }
    }
Instead of TestCS.

If I try with those classes, it deserializes fine as a single instance in the list:
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}]}";
Console.WriteLine("{0}", strjson);
RootObject x = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(strjson);
Testinfo ti = x.testinfo[0];
 
Share this answer
 
Comments
Richard Deeming 14-Feb-19 10:24am    
The class names don't matter, but the property names do. (testinfo vs TestInfoCollection) :)

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