如何将此 JSON 转换为数据表


[
    [
        "<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 字符串中找到的其他文本。

总数据兼容

  {
        "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)));

相关内容

  • 没有找到相关文章

最新更新