[英]How to convert this JSON to a datatable?
[
[
"<div id=\"status\" style=\"width:20px; height:20px; \" class=\"circle red\"></div>",
"<a runat=\"server\" id=\"link0\" href=\"MachineDetails.aspx?PageName=PCsByLocation.aspx&MachineName=AUBDW012\">AUBDW012</>",
"Dye, Paul",
"",
"AsiaPacific / Australia / BrisbaneDistribution",
"<div id=\"divonoffswitch\" class=\"onoffswitch\"><input type=\"checkbox\" name=\"onoffswitch\" onclick=\"javascript:hidedialoglabels();updateReasontext();spinnerOn();UpdateSpinner();__doPostBack('ContentPlaceHolder1_ucnBindSearchGrid_updReason','16800167:Auto-restart:'+this.checked+':AUBDW012:ContentPlaceHolder1_ucnBindSearchGrid_updValidation:myonoffswitch0')\" class=\"onoffswitch-checkbox\" id=\"myonoffswitch0\" checked=\"checked\" /><label class=\"onoffswitch-label\" for=\"myonoffswitch0\"> <span class=\"onoffswitch-inner\" ></span><span class=\"onoffswitch-switch\" ></span></label></div>",
" <input type=\"checkbox\" id=\"chkSelect0\" > "
],
[
"<div id=\"status\" style=\"width:20px; height:20px; \" class=\"circle red\"></div>",
"<a runat=\"server\" id=\"link1\" href=\"MachineDetails.aspx?PageName=PCsByLocation.aspx&MachineName=P03719-2K1\">P03719-2K1</>",
"Flint, Virginia",
"",
"AsiaPacific / Australia / BrisbaneDistribution",
"<div id=\"divonoffswitch\" class=\"onoffswitch\"><input type=\"checkbox\" name=\"onoffswitch\" onclick=\"javascript:hidedialoglabels();updateReasontext();spinnerOn();UpdateSpinner();__doPostBack('ContentPlaceHolder1_ucnBindSearchGrid_updReason','16797145:Auto-restart:'+this.checked+':P03719-2K1:ContentPlaceHolder1_ucnBindSearchGrid_updValidation:myonoffswitch1')\" class=\"onoffswitch-checkbox\" id=\"myonoffswitch1\" checked=\"checked\" /><label class=\"onoffswitch-label\" for=\"myonoffswitch1\"> <span class=\"onoffswitch-inner\" ></span><span class=\"onoffswitch-switch\" ></span></label></div>",
" <input type=\"checkbox\" id=\"chkSelect1\" > "
]
]
我目前正在使用这种方法进行转换。
DataTable dtValue = (DataTable)JsonConvert.DeserializeObject( outputJson, (typeof(DataTable)));
但是当我执行代码时,出现以下异常。 请指教。
完成反序列化对象后,在JSON字符串中找到其他文本。
JSON总数
{
"sEcho": 1,
"iTotalRecords": 16,
"iTotalDisplayRecords": 16,
"aaData": [
[
"<div id=\"status\" style=\"width:20px; height:20px; \" class=\"circle red\"></div>",
"<a runat=\"server\" id=\"link0\" href=\"MachineDetails.aspx?PageName=PCsByLocation.aspx&MachineName=AUBDW012\">AUBDW012</>",
"Dye, Paul",
"",
"AsiaPacific / Australia / BrisbaneDistribution",
"<div id=\"divonoffswitch\" class=\"onoffswitch\"><input type=\"checkbox\" name=\"onoffswitch\" onclick=\"javascript:hidedialoglabels();updateReasontext();spinnerOn();UpdateSpinner();__doPostBack('ContentPlaceHolder1_ucnBindSearchGrid_updReason','16800167:Auto-restart:'+this.checked+':AUBDW012:ContentPlaceHolder1_ucnBindSearchGrid_updValidation:myonoffswitch0')\" class=\"onoffswitch-checkbox\" id=\"myonoffswitch0\" checked=\"checked\" /><label class=\"onoffswitch-label\" for=\"myonoffswitch0\"> <span class=\"onoffswitch-inner\" ></span><span class=\"onoffswitch-switch\" ></span></label></div>",
" <input type=\"checkbox\" id=\"chkSelect0\" > "
],
[
"<div id=\"status\" style=\"width:20px; height:20px; \" class=\"circle red\"></div>",
"<a runat=\"server\" id=\"link1\" href=\"MachineDetails.aspx?PageName=PCsByLocation.aspx&MachineName=P03719-2K1\">P03719-2K1</>",
"Flint, Virginia",
"",
"AsiaPacific / Australia / BrisbaneDistribution",
"<div id=\"divonoffswitch\" class=\"onoffswitch\"><input type=\"checkbox\" name=\"onoffswitch\" onclick=\"javascript:hidedialoglabels();updateReasontext();spinnerOn();UpdateSpinner();__doPostBack('ContentPlaceHolder1_ucnBindSearchGrid_updReason','16797145:Auto-restart:'+this.checked+':P03719-2K1:ContentPlaceHolder1_ucnBindSearchGrid_updValidation:myonoffswitch1')\" class=\"onoffswitch-checkbox\" id=\"myonoffswitch1\" checked=\"checked\" /><label class=\"onoffswitch-label\" for=\"myonoffswitch1\"> <span class=\"onoffswitch-inner\" ></span><span class=\"onoffswitch-switch\" ></span></label></div>",
" <input type=\"checkbox\" id=\"chkSelect1\" > "
]
]}
您提供的Json字符串不是反序列化的DataTable
因此您无法将其直接转换为该对象类型。 不太确定是否绝对需要将其作为DataTable
,但是如果这样做,可以采用以下方法。
您可以首先将其放入字符串数组的数组中,如下所示:
var data = (string[][])JsonConvert.DeserializeObject(json, (typeof(string[][])));
现在,您可以从该数组构造数据表。 首先用所需的列创建一个DataTable
(不知道您到底需要什么,所以我将它们命名为Col1至Col7):
var dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Columns.Add("Col3");
dt.Columns.Add("Col4");
dt.Columns.Add("Col5");
dt.Columns.Add("Col6");
dt.Columns.Add("Col7");
现在,将已解析数组中的每个项目添加到表中:
Array.ForEach(data, r =>
{
var row = dt.NewRow();
row.ItemArray = r;
dt.Rows.Add(row);
}
);
由于您已经提供了完整的Json,因此可以创建一个模型来存储数据,如下所示:
public class DataObject
{
public int sEcho { get; set; }
public int iTotalRecords { get; set; }
public int iTotalDisplayRecords { get; set; }
public List<List<string>> aaData { get; set; }
}
您可以像这样反序列化为这种类型:
var data = (DataObject)JsonConvert.DeserializeObject(json, (typeof(DataObject)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.