Home > Software engineering >  Json failing to mesh with C# Model class
Json failing to mesh with C# Model class

Time:10-22

Json is generated in an ajax call from the view and then sent to the controller, unfortunately the code is not deserialising into an instance of a model and is giving me a null value The json -

{
    "CheckSheetViewModel": {
        "LstCheckSheetQuestion": {
            "TblCheckSheetQuestion": [{
                "CheckSheetQuestionId": "9",
                "DateCreated": "04/11/2015 23:37:45",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Traffic Lights",
                "QuestionText": "Traffic lights and notes image upload test",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "0",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "10",
                "DateCreated": "04/11/2015 23:38:21",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Traffic Lights",
                "QuestionText": "Traffic lights question",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "This is tip for this question",
                "DisplayOrder": "1",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "406",
                "DateCreated": "04/04/2016 09:55:30",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Image Upload",
                "QuestionText": "My test",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "3009",
                "CheckSheetGroupId": "36",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "407",
                "DateCreated": "04/04/2016 10:18:34",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Yes No",
                "QuestionText": "This is question 3",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "2",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "440",
                "DateCreated": "02/12/2016 17:57:04",
                "CreatedByUserId": "2",
                "CheckSheetId": "2",
                "QuestionType": "Image Upload",
                "QuestionText": "Another image question",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "1008",
                "CheckSheetGroupId": "34",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "5180",
                "DateCreated": "23/08/2021 08:44:17",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Traffic Lights",
                "QuestionText": "test",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "3",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "5181",
                "DateCreated": "23/08/2021 08:45:07",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Yes No",
                "QuestionText": "test 2",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "4",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "5182",
                "DateCreated": "23/08/2021 08:46:04",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Image Upload",
                "QuestionText": "test question",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "5",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "5183",
                "DateCreated": "23/08/2021 08:51:35",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Traffic Lights",
                "QuestionText": "question 10",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "6",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }, {
                "CheckSheetQuestionId": "5184",
                "DateCreated": "23/08/2021 08:51:35",
                "CreatedByUserId": "1",
                "CheckSheetId": "2",
                "QuestionType": "Serviceability",
                "QuestionText": "question 11",
                "AllowNotes": "",
                "IncludeFollowUpDate": "",
                "AllowSkip": "",
                "AllowSupportingImages": "",
                "QuestionTip": "",
                "DisplayOrder": "7",
                "CheckSheetGroupId": "",
                "VehicleVisualsVideoId": "",
                "VehicleVisualsVideoTitle": ""
            }]
        },
        "LstCheckSheetQuestionGroup": {
            "TblCheckSheetQuestionGroup": [{}, {
                "CheckSheetGroupId": "34",
                "DateCreated": "04/04/2016 09:55:05",
                "Name": "Under The Bonnet",
                "AnswerAsGroup": "true",
                "DisplayOrder": "1"
            }, {
                "CheckSheetGroupId": "35",
                "DateCreated": "04/04/2016 09:55:06",
                "Name": "Tyre Report",
                "AnswerAsGroup": "true",
                "DisplayOrder": "3"
            }, {
                "CheckSheetGroupId": "36",
                "DateCreated": "04/04/2016 10:18:04",
                "Name": "Brake Report",
                "AnswerAsGroup": "true",
                "DisplayOrder": "4"
            }, {
                "CheckSheetGroupId": "37",
                "DateCreated": "04/04/2016 10:18:04",
                "Name": "Vehicle Raised",
                "AnswerAsGroup": "true",
                "DisplayOrder": "5"
            }, {
                "CheckSheetGroupId": "39",
                "DateCreated": "04/04/2016 10:18:04",
                "Name": "To Finish",
                "AnswerAsGroup": "true",
                "DisplayOrder": "7"
            }, {
                "CheckSheetGroupId": "42",
                "DateCreated": "04/04/2016 10:18:34",
                "Name": "Vehicle Lowered",
                "AnswerAsGroup": "true",
                "DisplayOrder": "6"
            }]
        },
        "LstCheckSheet": {
            "TblCheckSheet": [{
                "CheckSheetId": "2",
                "Description": "Amateur Service",
                "DateCreated": "04/11/2015 23:37:45",
                "CreatedByUserId": "1",
                "Enabled": "true",
                "StartMessage": "",
                "EndMessage": "",
                "AnswerInSequence": "False",
                "AddToAllNewJobs": "True",
                "MustBeSignedOff": "true"
            }]
        }
    }
}

is being sent to this controller item -

 [HttpPost]
        
        public ActionResult checkSheetUpdate([FromBody] CheckSheetViewModel data)
        {
        //do stuff
        }
    

using this model -

  public partial class CheckSheetViewModel
{
    [JsonProperty("LstCheckSheetQuestionGroup")]
    [JsonPropertyName("LstCheckSheetQuestionGroup")]
    public List<TblCheckSheetQuestionGroup> LstCheckSheetQuestionGroup { get; set; }
    [JsonProperty("LstCheckSheetQuestion")]
    [JsonPropertyName("LstCheckSheetQuestion")]
    public List<TblCheckSheetQuestion> LstCheckSheetQuestion { get; set; }
    [JsonProperty("LstCheckSheet")]
    [JsonPropertyName("LstCheckSheet")] 
    public List<TblCheckSheet> LstCheckSheet { get; set; }
    [JsonProperty("TblCheckSheet")]
    [JsonPropertyName("TblCheckSheet")]
    
    public TblCheckSheet TblCheckSheet { get; set; }
    [JsonProperty("User")]
    [JsonPropertyName("User")] 
    public TblUser User { get; set; }
    [JsonProperty("Vidview")]
    [JsonPropertyName("Vidview")]
    public VideoViewModel Vidview { get; set; }
    [JsonProperty("Animation")]
    [JsonPropertyName("Animation")]
    public Animation Animation { get; set; }


}

All the name and cases match and yet I still get a null on the input (data). Am I barking up the wrong tree or is there something obviously wrong? The model is made up of some lists of sub-models (not shown) for brevity.

CodePudding user response:

the root of your model needs to be fixed

    Root myModel = JsonConvert.DeserializeObject<Root>(json);

    public class Root
    {
        public CheckSheetViewModel CheckSheetViewModel { get; set; }
    }

    public class CheckSheetViewModel
    {
        public LstCheckSheetQuestion LstCheckSheetQuestion { get; set; }
        public LstCheckSheetQuestionGroup LstCheckSheetQuestionGroup { get; set; }
        public LstCheckSheet LstCheckSheet { get; set; }
    }

    public class LstCheckSheet
    {
        public List<TblCheckSheet> TblCheckSheet { get; set; }
    }

    public class LstCheckSheetQuestion
    {
        public List<TblCheckSheetQuestion> TblCheckSheetQuestion { get; set; }
    }

    public class LstCheckSheetQuestionGroup
    {
        public List<TblCheckSheetQuestionGroup> TblCheckSheetQuestionGroup { get; set; }
    }

   

CodePudding user response:

Right, seems to be more to do with Multidimensional arrays and nullable Booleans (as I said legacy data), with a possible side order of data types

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

I will reappraise my approach.

  • Related