skip to Main Content

I have a JSON string with Multiple JSON Object. I would like to know how to deserialize Json string to DataSet.

I have deserialize JSON string to DataTable. It works fine but I would like to know how to convert multiple JSON object to dataset

Below is sample JSON:

{
"Name": "Jamal",
"Age": "42",
"City": "Mumbai",
"EducationalInfo": [{
    "Degree": "BE",
    "University": "Cristian Universiy"
}, {
    "Degree": "ME",
    "University": "Texas University"
}],
"LoanInfo": [{
    "LoanType": "Car Loan",
    "Amount": "1000000"
}, {
    "LoanType": "Personal",
    "Amount": "200000"
}]
}

Expected Output is:

Name, Age, City is one datatable

Degree & University is one datatable

LoanType & Amount is one datatable

Please suggest how to covert this.

2

Answers


  1. First, you want to create a model to deserialize your JSON string. In your case, your model would look as follow:

    public class Root
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
        public EducationalInfo[] EducationalInfo { get; set; }
        public LoanInfo[] LoanInfo { get; set; }
    }
    
    public class EducationalInfo
    {
        public string Degree { get; set; }
        public string University { get; set; }
    }
    
    public class LoanInfo
    {
        public string LoanType { get; set; }
        public int Amount { get; set; }
    }
    

    Then you can deserialize it by doing the following:

    Root root = JsonSerializer.Deserialize<Root>(yourJsonString);
    

    Where root will be your object filled with the content of the JSON string.

    Login or Signup to reply.
  2. To convert the given JSON string into a DataSet with multiple DataTables, you can utilize the Newtonsoft.Json library in C#.
    Use below class to deserialize the JSON string.

    Rootobject rootobject = JsonConvert.DeserializeObject(jsonString);

      public class Rootobject
        {
            public string Name { get; set; }
            public string Age { get; set; }
            public string City { get; set; }
            public Educationalinfo[] EducationalInfo { get; set; }
            public Loaninfo[] LoanInfo { get; set; }
        }
    
        public class Educationalinfo
        {
            public string Degree { get; set; }
            public string University { get; set; }
        }
    
        public class Loaninfo
        {
            public string LoanType { get; set; }
            public string Amount { get; set; }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search