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

时间:2015-06-17 13:33:48

标签: c# .net json asynchronous json.net

[
    [
        "<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\" > "
            ]
                ]}

1 个答案:

答案 0 :(得分:0)

您提供的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)));
相关问题