Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Friends
How can i convert JSON to Data table

What I have tried:

<pre>Dear Friends,
I have Json Data, which I got after convert Data Table to Json by 
<pre>Jayrock.Json.Conversion.JsonConvert.ExportToString(oDataTable)

{"columns":["ID","Dept_Name"],"rows":[[1,"Edu Dept"],[2,"Clg Dept"],[3,"Uni Dept"],[4,"Lib Dept"]
]} 

How i can convert it revert back as DataTable again.

Thanks in Advance
Posted
Updated 23-Nov-18 22:07pm
v2

1 solution

I don't use Jayrock - heck I've never heard of it before - but I use Newtonsoft, and it works fine:
C#
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Dept_Name");
dt.Rows.Add(1, "Edu Dept");
dt.Rows.Add(2, "Clg Dept");
dt.Rows.Add(3, "Uni Dept");
dt.Rows.Add(4, "Lib Dept");

string json = JsonConvert.SerializeObject(dt);

DataTable dt2 = JsonConvert.DeserializeObject<DataTable>(json);
I would assume that Jayrock JSON has the same kind of facility.
Be aware that your JSON doesn't look like mine:
[{"ID":"1","Dept_Name":"Edu Dept"},{"ID":"2","Dept_Name":"Clg Dept"},{"ID":"3","Dept_Name":"Uni Dept"},{"ID":"4","Dept_Name":"Lib Dept"}]
Is what Newtonsoft gives me, and it gives errors on your JSON string - you may want to look around and see what you are going to use it with if you are planning on sharing the JSON data with others.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900