Search code examples
c#jsondeserializationjson-deserialization

Deserialize complex Json using c#


How to deserialize the below JSON using c#

string data = @"{""root"": {    ""EmployeeMaster"": {                    ""EmployeeMasterData"": [                      {          ""ContactDetails"": {            ""ContactDetail"": [                            {                ""BKPLZ"": ""1"",                              ""Action"": ""Create"",                              ""GBDEP"": ""P"",                              ""PERNR"": ""1"",                              ""AddressType"": ""Per"",                              ""BKORT"": ""P,""              },              {                ""BKPLZ"": ""262228"",                ""Action"": ""Create"",                ""GBDEP"": ""p"",                ""PERNR"": ""1"",                ""AddressType"": ""Res"",                ""BKORT"": ""p""              }            ]          },          ""BankDetails"": {            ""BankDetail"": {              ""ZBANKGRP"": ""B"",              ""EMFTX"": """",              ""BANKN"": ""123"",              ""Action"": ""Create"",              ""PERNR"": ""1"",              ""ZZIFSC"": ""123"",              ""ZLSCH"": """"            }          },          ""BasicDetails"": {            ""BasicDetail"": {              ""ANREX"": ""Mr."",        ""GBLND"": ""IND"",              ""PLANS"": """",              ""FRO"": ""3"",              ""Action"": ""Create"",              ""BEGDA"": ""2008-04-15"",              ""PERNR"": ""1"",              ""IO"": ""2"",              ""WERKS"": ""Corporate"",              ""MASSG"": """",              ""KST01"": ""PDLH201"",              ""GSBER"": ""CFHO"",              ""VORNA"": ""Kapil"",              ""ORT01"": ""Noida"",              ""NATIO"": ""IND"",              ""NACHIN"": ""M"",              ""PERSK"": ""MG4"",              ""GESCH"": ""Male"",              ""PERSG"": ""Active"",              ""ABKRS"": ""CM"",              ""BTRTAL"": ""N"",              ""GBDAT"": ""1980-07-28"",              ""RO"": ""2"",              ""KBU01"": ""L""            }          }        }      ]    }  }}";


var json = JObject.Parse(data);

var serializer = new JsonSerializer();
var a= serializer.Deserialize<EmployeeMasterData>(json["root"]["EmployeeMaster"].CreateReader());

public class EmployeeMasterData
{
    public List<List<ContactDetail>> ContactDetails { get; set; }
    public List<List<BankDetail>> BankDetails { get; set; }
    public List<List<BasicDetail>> BasicDetails { get; set; }
}
public class BankDetail
{
    public string ZBANKGRP { get; set; }
    public string EMFTX { get; set; }
    public string BANKN { get; set; }
    public string Action { get; set; }
    public string PERNR { get; set; }
    public string ZZIFSC { get; set; }
    public string ZLSCH { get; set; }
}
public class ContactDetail
{
    public string BKPLZ { get; set; }
    public string Action { get; set; }
    public string GBDEP { get; set; }
    public string PERNR { get; set; }
    public string AddressType { get; set; }
    public string BKORT { get; set; }
}
public class BasicDetail
{
    public string ANREX { get; set; }
    public string GBLND { get; set; }
    public string PLANS { get; set; }
    public string FRO { get; set; }
    public string Action { get; set; }
    public string BEGDA { get; set; }
    public string PERNR { get; set; }
    public string IO { get; set; }
    public string WERKS { get; set; }
    public string MASSG { get; set; }
    public string KST01 { get; set; }
    public string GSBER { get; set; }
    public string VORNA { get; set; }
    public string ORT01 { get; set; }
    public string NATIO { get; set; }
    public string NACHIN { get; set; }
    public string PERSK { get; set; }
    public string GESCH { get; set; }
    public string PERSG { get; set; }
    public string ABKRS { get; set; }
    public string BTRTAL { get; set; }
    public string GBDAT { get; set; }
    public string RO { get; set; }
    public string KBU01 { get; set; }
}

Solution

  • If you intend to deserialize entire Json, your class definition needs some changes. For example,

    public class ContactDetail
    {
        public string BKPLZ { get; set; }
        public string Action { get; set; }
        public string GBDEP { get; set; }
        public string PERNR { get; set; }
        public string AddressType { get; set; }
        public string BKORT { get; set; }
    }
    
    public class ContactDetails
    {
        public List<ContactDetail> ContactDetail { get; set; }
    }
    
    public class BankDetail
    {
        public string ZBANKGRP { get; set; }
        public string EMFTX { get; set; }
        public string BANKN { get; set; }
        public string Action { get; set; }
        public string PERNR { get; set; }
        public string ZZIFSC { get; set; }
        public string ZLSCH { get; set; }
    }
    
    public class BankDetails
    {
        public BankDetail BankDetail { get; set; }
    }
    
    public class BasicDetail
    {
        public string ANREX { get; set; }
        public string GBLND { get; set; }
        public string PLANS { get; set; }
        public string FRO { get; set; }
        public string Action { get; set; }
        public string BEGDA { get; set; }
        public string PERNR { get; set; }
        public string IO { get; set; }
        public string WERKS { get; set; }
        public string MASSG { get; set; }
        public string KST01 { get; set; }
        public string GSBER { get; set; }
        public string VORNA { get; set; }
        public string ORT01 { get; set; }
        public string NATIO { get; set; }
        public string NACHIN { get; set; }
        public string PERSK { get; set; }
        public string GESCH { get; set; }
        public string PERSG { get; set; }
        public string ABKRS { get; set; }
        public string BTRTAL { get; set; }
        public string GBDAT { get; set; }
        public string RO { get; set; }
        public string KBU01 { get; set; }
    }
    
    public class BasicDetails
    {
        public BasicDetail BasicDetail { get; set; }
    }
    
    public class EmployeeMasterData
    {
        public ContactDetails ContactDetails { get; set; }
        public BankDetails BankDetails { get; set; }
        public BasicDetails BasicDetails { get; set; }
    }
    
    public class EmployeeMaster
    {
        public List<EmployeeMasterData> EmployeeMasterData { get; set; }
    }
    
    public class Root
    {
        public EmployeeMaster EmployeeMaster { get; set; }
    }
    
    public class RootObject
    {
        public Root root { get; set; }
    }
    

    Now you could deserialize as

    var result = JsonConvert.DeserializeObject<RootObject>(data);
    

    If you intend to deserialize only a part of it, for example, as seen in the OP, the EmployeeMaster alone, you could do the following

    var json = JObject.Parse(data);
    var result= JsonConvert.DeserializeObject<EmployeeMaster>(json["root"]["EmployeeMaster"].ToString());