簡體   English   中英

如何將此JSON轉換為數據表?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM