unable to deserialization Jason file though got the results upto response

41 Views Asked by At

jason file =

 {
    "educations": {
        "count": 23,
        "education": [{
                    "studId": "",
                    "joinDate": "2021-04-08 12:22",
                    "regDate": "2021-04-08 12:23",
                    "enrolled": false,
                    "eduId": "1-eassasa7",
                    "pId": "dfdgdfg456fghf",
                    "startDate": "2021-09-03",
                    "expiry": {
                        "monthlyExpire": true
                    },
                    "reRegistration": "OPTIONAL",
                    "refreeId1": "",
                    "refreeId2": "",
                    "tutor": true,
                    "libraryAllowed": true,
                    "sports": "ENABLE",
                    "sendNotification": true,
                    "ccEmail": "",
                    "isTest": false,
                    "SportsGroup"

above is my jason and I was able to get that in to the response

string rawResponse = apiDataResponse.Content;
var result = JsonConvert.DeserializeObject<RootData>(rawResponse);

below are my classes

  RootData
    {
        [JsonProperty(PropertyName ="educations")]
        public education[] edu1 { get; set; } 
    }

class educationss
{
    public int count { get; set; }    
    [JsonProperty(PropertyName="education")]
    public education[] edu { get; set; }    
}


class education
    {         
        public string studId { get; set; }      
        public DateTime joinDate { get; set; }
        public DateTime regDate { get; set; }
        public bool enrolled { get; set; }
        public string eduId { get; set; }
        public string pId { get; set; }
        public DateTime startDate { get; set; }
        public expiry[] monthlyExpire { get; set; }
        public string reRegistration { get; set; }
        public string refreeId1 { get; set; }
        public string refreeId2 { get; set; }
        public bool tutor { get; set; }
        public bool LibraryAllowed { get; set; }
        public string sports { get; set; }
        public bool sendNotification { get; set; }
        public string ccEmail { get; set; }
        public bool isTest { get; set; }
        public sportGroup[] sportGroup { get; set; }
    }

But I am unable to get any value for my result variable and it shows null.

Can you please help me?
1

There are 1 best solutions below

0
On

try this

var json= ...json string
var jsonDeserialized = JsonConvert.DeserializeObject<DataRoot>(json); 

jsonDeserialized.educations.education.ForEach(e =>
{
  Console.WriteLine($"joinDate: {e.joinDate.ToString()},   pid: {e.pId}");
});

classes

public class DataRoot
{
    public Educations educations { get; set; }
}


public class Educations
{
    public int count { get; set; }
    public List<Education> education { get; set; }
}
public class Education
{
    public string studId { get; set; }
    public string joinDate { get; set; }
    public string regDate { get; set; }
    public bool enrolled { get; set; }
    public string eduId { get; set; }
    public string pId { get; set; }
    public string startDate { get; set; }
    public Expiry expiry { get; set; }
    public string reRegistration { get; set; }
    public string refreeId1 { get; set; }
    public string refreeId2 { get; set; }
    public bool tutor { get; set; }
    public bool libraryAllowed { get; set; }
    public string sports { get; set; }
    public bool sendNotification { get; set; }
    public string ccEmail { get; set; }
    public bool isTest { get; set; }
    public object SportsGroup { get; set; }
}

public class Expiry
{
    public bool monthlyExpire { get; set; }
}

you have to fix your json

{
  "educations": {
    "count": 23,
    "education": [
      {
        "studId": "",
        "joinDate": "2021-04-08 12:22",
        "regDate": "2021-04-08 12:23",
        "enrolled": false,
        "eduId": "1-eassasa7",
        "pId": "dfdgdfg456fghf",
        "startDate": "2021-09-03",
        "expiry": {
          "monthlyExpire": true
        },
        "reRegistration": "OPTIONAL",
        "refreeId1": "",
        "refreeId2": "",
        "tutor": true,
        "libraryAllowed": true,
        "sports": "ENABLE",
        "sendNotification": true,
        "ccEmail": "",
        "isTest": false,
        "SportsGroup": null
      }
    ]
  }
}