Home > Enterprise >  Returning null value in JSON Deserialization
Returning null value in JSON Deserialization

Time:02-24

I am trying to deserialize the json string in C#. Following is jsonstring I used. But after deserialize it is giving null value. So what's going wrong with me?

string res = @"{   ""root"":{""EmployeeMaster"":{""EmployeeMasterData"":[{""BasicDetails"":{ ""BasicDetail"":{ ""Action"":""Update"",""EmployeeCode"":""0076"",""L2ManagerCode"":2911}}},{""BasicDetails"":{""BasicDetail"":{ ""Designation"":""Branch Incharge"",""Action"":""Update"",""EmployeeCode"":1786,""SubDepartment"":""Branch Manager"",""Department"":""Operations"",""JOBROLE"":""Branch Manager"",}}},{""DependentDetails"":{""DependentDetail"":{""DateOfBirth"":""1972-07-31"",""DependentName"":""Ramani"",""Action"":""Create"",""EmployeeCode"":2923,""Address"":"""",""RelationshipType"":""Mother"",""Gender"":"""",""IsDependent"":""No""}}},{""ContactDetails"":{""ContactDetail"":[{""AddressLine2"":""Turvekere"",""AddressLine1"":""Hallada Hosahalli"",""Action"":""Update"",""Country"":""India"",""City"":""Tumkur"",""Pincode"":572227},{""AddressLine2"":"""",""AddressLine1"":"""",""Action"":""Update"",""State"":"""",""Country"":"""",""City"":"""",""Pincode"":""""}]}},{""BasicDetails"":{""BasicDetail"":{""Action"":""Create"",""L2ManagerName"":""Ratheesh P R"",""IsDisabled"":""No"",""L1ManagerCode"":2771,""SubDepartment"":""Collection Assistant"",""Gender"":""Male"",""EmploymentType"":""Permanent"",""Department"":""Operations"",""L1ManagerName"":""Arun K K"",""Designation"":""Collection Assistant"",""EmployeeCode"":201021,""L2ManagerCode"":2564,""FirstName"":""Vishnu"",""Title"":""Mr."",""EmploymentStatus"":""Active"",""MiddleName"":"""",""DisabilityType"":"""",""OfficialMailID"":"""",""Nationality"":""IND"",""DateOfRelieving"":"""",""DateOfJoining"":""2022-01-20"",""JOBROLE"":"""",""LastName"":""G"",""BirthDate"":""1995-08-28""}}}]}}}";

It's not working, because the primitive object is invalid. can anyone please help me to create the class file. thanks in advance

CodePudding user response:

try this, it was tested in visual studio

var jsonParsed = JObject.Parse(json); 

List<EmployeeMasterData> employeeMasterData  = jsonParsed["root"]["EmployeeMaster"]
["EmployeeMasterData"].Select(x => AddItems(x)).ToList();

public static EmployeeMasterData AddItems(JToken jt)
{
    var emd = new EmployeeMasterData();
    foreach (var element in ((JObject)jt).Properties())
    {
        if (element.Name == "BasicDetails")
            emd.BasicDetails = element.Value.ToObject<BasicDetails>();
        if (element.Name == "DependentDetails")
            emd.DependentDetails = element.Value.ToObject<DependentDetails>();
        if (element.Name == "ContactDetails")
            emd.ContactDetails = element.Value.ToObject<ContactDetails>();
    }
    return emd;
}

classes

public class EmployeeMasterData
{

    public BasicDetails BasicDetails { get; set; }

    public DependentDetails DependentDetails { get; set; }

    public ContactDetails ContactDetails { get; set; }
}

public class EmployeeMaster
{
    public List<EmployeeMasterData> EmployeeMasterData { get; set; }
}

public class Root
{
    public EmployeeMaster EmployeeMaster { get; set; }
}
public class BasicDetail
{
    public string Action { get; set; }
    public string EmployeeCode { get; set; }
    public int? L2ManagerCode { get; set; }
    public string Designation { get; set; }
    public string SubDepartment { get; set; }
    public string Department { get; set; }
    public string JOBROLE { get; set; }
    public string L2ManagerName { get; set; }
    public string IsDisabled { get; set; }
    public string L1ManagerCode { get; set; }
    public string Gender { get; set; }
    public string EmploymentType { get; set; }
    public string L1ManagerName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }
    public string EmploymentStatus { get; set; }
    public string MiddleName { get; set; }
    public string DisabilityType { get; set; }
    public string OfficialMailID { get; set; }
    public string Nationality { get; set; }
    public string DateOfRelieving { get; set; }
    public string DateOfJoining { get; set; }
    public string LastName { get; set; }
    public string BirthDate { get; set; }
}

public class BasicDetails
{
    public BasicDetail BasicDetail { get; set; }
}

public class DependentDetail
{
    public string DateOfBirth { get; set; }
    public string DependentName { get; set; }
    public string Action { get; set; }
    public string EmployeeCode { get; set; }
    public string Address { get; set; }
    public string RelationshipType { get; set; }
    public string Gender { get; set; }
    public string IsDependent { get; set; }
}

public class DependentDetails
{
    public DependentDetail DependentDetail { get; set; }
}

public class ContactDetail
{
    public string AddressLine2 { get; set; }
    public string AddressLine1 { get; set; }
    public string Action { get; set; }
    public string Country { get; set; }
    public string City { get; set; }
    public object Pincode { get; set; }
    public string State { get; set; }
}

public class ContactDetails
{
    public List<ContactDetail> ContactDetail { get; set; }
}


CodePudding user response:

Generally, I would suggest the NewtonSoft JSON library if you are working with JSON. You can serialize and deserialize JSON to class, and class to JSON.

With this library you must not parse JSON by yourself.

JSON Library

CodePudding user response:

First of all, there are some errors in the Json format. There is an incorrect use of the icon. Additionally, if you specify string for Json, the \ add symbol is required for each ".

You do not need to write @ in front of the string this time.

like this

\"DateOfJoining\":\"2022-01-20\"
  • Related