DataTable轉換成json格式兩則

2021-06-16 22:46:48 字數 2067 閱讀 9637

通過datatable 返回json格式,用於繫結easyui tree

/// /// 根據datatable生成json樹結構

///

/// 資料來源

/// id列

/// text列

/// 關係字段(字典表中的樹結構字段)

/// 父id(0)

stringbuilder result = new stringbuilder();

stringbuilder sb = new stringbuilder();

private void gettreejsonbytable(datatable tabel, string idcol, string txtcol, string rela, object pid)

=''", rela, pid);

datarow rows = tabel.select(filer);

if (rows.length > 0)

=''", rela, row[idcol])).length > 0)

sb.clear();

}sb = sb.remove(sb.length - 1, 1);

}sb.clear();

}}

gettreejsonbytable(datatable, "

id", "

title

", "

pid", "

0");

string content = result.tostring();

通過datatable 返回json格式,用於繫結easyui tree/// /// 根據datatable生成json樹結構  呼叫gettreejsonbytable(datatable, "menuid", "menuname", "parentid", "0");

/// 

/// 資料來源

/// id列

/// text列

/// 關係字段

/// 父id

///easyui tree json格式

stringbuilder treeresult = new stringbuilder();

stringbuilder treesb = new stringbuilder();

public string gettreejsonbytable(datatable tabel, string idcol, string txtcol, string rela, object pid)

is null", rela);

}else

=''", rela, pid);

}datarow rows = tabel.select(filer);

if (rows.length > 0)

=''", rela, row[idcol])).length > 0)

treesb.clear();

}treesb = treesb.remove(treesb.length - 1, 1);

}treejson = treeresult.tostring();

treesb.clear();

}return treejson;

}

呼叫方法:

menutreehelper treehelper = new menutreehelper();

return treehelper.gettreejsonbytable(datatable, "id", "text", "parentno", "0");

把datatable 轉換成json格式,適用於easyui 繫結datagrid

public static string datatable2json(system.data.datatable dt)

jsonbuilder.remove(jsonbuilder.length - 1, 1);

}jsonbuilder.remove(jsonbuilder.length - 1, 1);

return jsonbuilder.tostring();

實體陣列轉換成DataTable

public static datatable todatatable listentitys 取出第乙個實體的所有propertie type entitytype entitys 0 gettype propertyinfo entityproperties entitytype.getprop...

IList類轉換成DataTable

將集合類轉換成datatable 集合 public static datatable todatatable ilist list for int i 0 i list.count i object array templist.toarray result.loaddatarow array,t...

rails hash轉換成前端可用的json

如果說,在home controller.rb裡的index方法中定義了 data 那麼通過 data 傳到前端的js會解析成 var arr data var arr var arr raw data var arr 然後呢,你嘗試在index那個action裡用 data 這麼定義 覺得會把 a...