-4

IDE: Visual Studio 2010 Platform: C#

I am trying to convert the JSON object which consist of data in two different tables into Data table. Here is the code I have written:

JavaScriptSerializer objJSSWW = new JavaScriptSerializer();
var lastBattingDetailsWW = JsonConvert.DeserializeObject<EmpDetails>(JsonBattingWW);


public class EmpDetails
{
    public List<List<object>> Table { get; set; }
    public List<List<object>> Table1 { get; set; }
}

I want these two tables to be stored in DataTable. Any Suggestions ?

stefankmitph
  • 3,236
  • 2
  • 18
  • 22
Harsh Singhi
  • 117
  • 2
  • 5
  • 14

1 Answers1

0

Trying to give you some (at least pseudo-)code. Assuming you have a fixed length of your 'List's. For this example I'm assuming your List contains 3 elements.

DataTable dataTable1 = new DataTable();
dataTable1.Columns.Add("col1");
dataTable1.Columns.Add("col2");
dataTable1.Columns.Add("col3");
foreach (var row in EmpDetails.Table)
{
    dataTable1.Rows.Add(row.ToArray());
}

DataTable dataTable2 = new DataTable();
dataTable2.Columns.Add("col1");
dataTable2.Columns.Add("col2");
dataTable2.Columns.Add("col3");
foreach (var row in EmpDetails.Table1)
{
    dataTable2.Rows.Add(row.ToArray());
}

If you have 2 tables with the same columns you can simply merge them:

dataTable1.Merge(dataTable2);
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
  • I appreciate this answer , but this will take a lot of time if there are some lakhs of records. I am looking for some way to deserialize it in one go instead of looping through all the records... – Harsh Singhi Mar 17 '15 at 10:06
  • deserialization also does loop through it... somehow. – stefankmitph Mar 17 '15 at 10:07