C# 如何将此JSON转换为数据表?

C# 如何将此JSON转换为数据表?,c#,.net,json,asynchronous,json.net,C#,.net,Json,Asynchronous,Json.net,但是当我执行代码时,我得到了以下异常。请告知。 完成对象反序列化后,在JSON字符串中找到其他文本。 总JSON DataTable dtValue = (DataTable)JsonConvert.DeserializeObject( outputJson, (typeof(DataTable))); { "sEcho":1,, “iTotalRecords”:16, “iTotalDisplayRecords”:16, “aaData”:[ [ "", “AUBDW012”, “戴,保罗

但是当我执行代码时,我得到了以下异常。请告知。
完成对象反序列化后,在JSON字符串中找到其他文本。

总JSON

DataTable dtValue = (DataTable)JsonConvert.DeserializeObject( outputJson, (typeof(DataTable)));
{
"sEcho":1,,
“iTotalRecords”:16,
“iTotalDisplayRecords”:16,
“aaData”:[
[
"",
“AUBDW012”,
“戴,保罗”,
"",
“亚太地区/澳大利亚/布里斯班分销”,
" ",
"  "
],
[
"",
“P03719-2K1”,
“弗林特,弗吉尼亚”,
"",
“亚太地区/澳大利亚/布里斯班分销”,
" ",
"  "
]
]}

您提供的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\" > "
            ]
                ]}
Array.ForEach(data, r => 
    {
        var row = dt.NewRow();
        row.ItemArray = r;
        dt.Rows.Add(row);
    }
);
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; }
}
现在可以从这个数组构造数据表了。首先创建一个包含所需列的
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");
编辑 由于您现在已经提供了完整的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\" > "
            ]
                ]}
Array.ForEach(data, r => 
    {
        var row = dt.NewRow();
        row.ItemArray = r;
        dt.Rows.Add(row);
    }
);
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; }
}

那不是JSON,那只是一个字符串数组。其次,它的格式不正确,无法反序列化为
DataTable
。您应该有一个对象数组,例如
[{“prop1”:“value1”},{“prop2”:“value2”},…]
prop
字段是列名,
value
字段是列值。@Cᴏʀ基本上是json。。。有点奇怪。。。但是它是有效的json。您也可以像这样使用
List
而不是字符串数组:
var List=JsonConvert.DeserializeObject(outputJson)
@stefankmitph:我知道我在这里可能理解得有点过于字面,因为我知道代码可能是一个字符串,并且被分配给
outputJson
,但JSON是一个符号,
字符串
,上面的代码是一个文本JS数组。@Cᴏ你完全正确。