首先下载Newtonsoft.Json.dll文件,并引用到项目中(注意要与frameWork版本对应);
然后在类中引用一下命名空间;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
方法:
public string GetDisInfo()
{
DataTable tblDatas = new DataTable("Datas");
DataColumn dc = null;
dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
dc.AutoIncrement = true;//自动增加
dc.AutoIncrementSeed = 1;//起始为1
dc.AutoIncrementStep = 1;//步长为1
dc.AllowDBNull = false;//
dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
DataRow newRow;
newRow = tblDatas.NewRow();
newRow["Product"] = "犬夜叉";
newRow["Version"] = "2.0";
newRow["Description"] = "动漫";
tblDatas.Rows.Add(newRow);
newRow = tblDatas.NewRow();
newRow["Product"] = "西游记";
newRow["Version"] = "3.0";
newRow["Description"] = "名著";
tblDatas.Rows.Add(newRow);
return JsonConvert.SerializeObject(tblDatas, new DataTableConverter());
}