JSON To DataTable 带js小数点四舍五入数字 被四舍五入了?请问这是怎么回事

c#常用的Datable转换为json,以及json转换为DataTable操作方法 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 11时,
/*==============================================================================**&Filename:&DatatableToJson.cs*&Description:&主要包含两个方法:&&&1.&获取的DataTable&对象&转换为Json&字符串&&&2.&Json&字符串&转换为&DataTable数据集合*&Version:&1.0*&Created:&*&Author:&liangjw*&E-mail&:&*&Q&&&Q&&&:&*&Profile&Url:http://90ideas.net/*&Company:&Copyright&(C)&Create&Family&Wealth&Power&By&Peter*==============================================================================*/&*&备注信息:&上传部分自己总结的常用方法的封装,有不足和不完美之处,希望大家指出来,愿意一起&*&主要研究erp,cms,crm,b2b,oa等系统和网站的开发,欢迎有共同追求和学的IT人员一起学习和交流。&*&学习和讨论有关asp.net&&mvc&,Ajax&,jquery&,html/css,&xml&,sqlserver&,wpf,IIS以及服务器的搭建和安全性相关技术的交流和学习。
代码片段(1)
1.&[代码][C#]Datatable和json互相转换操作&&&&
DataTable 转换为Json字符串实例方法
/// &summary&
/// GetClassTypeJosn 的摘要说明
/// &/summary&
public class GetClassTypeJosn : IHttpHandler
/// &summary&
/// 文件名:DataTable 和Json 字符串互转
/// 版权所有:Copyright (C) Create Family Wealth liangjw
/// 创建标示:
/// &/summary&
//用法说明实例
public void ProcessRequest(HttpContext context)
context.Response.ContentType = "application/json";
context.Response.Charset = "utf-8";
HttpRequest req = context.R
string method = req["method"].ToStr().ToLower();
//获取合同明细列表
DataTable 转换为Json字符串
if (method == "txtdate")
string json = "";
BO.MakeContractMx bll = new MakeContractMx();
DataSet ds = bll.GetDataTable();
if (ds.Tables.Count & 0)
json =ToJson(ds.Tables[0]);
context.Response.Write(json);
public bool IsReusable
#endregion
#region Json字符串转换为DataTable 实例方法
public DataTable JsonToDataTable(json)
dt= ToDataTable(json);
#endregion
#region DataTable 转换为Json 字符串
/// &summary&
/// DataTable 对象 转换为Json 字符串
/// &/summary&
/// &param name="dt"&&/param&
/// &returns&&/returns&
public static string ToJson(this DataTable dt)
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxV //取得最大数值
ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
Dictionary&string, object& dictionary = new Dictionary&string, object&();
//实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
arrayList.Add(dictionary); //ArrayList集合中添加键值
return javaScriptSerializer.Serialize(arrayList);
//返回一个json字符串
#endregion
#region Json 字符串 转换为 DataTable数据集合
/// &summary&
/// Json 字符串 转换为 DataTable数据集合
/// &/summary&
/// &param name="json"&&/param&
/// &returns&&/returns&
public static DataTable ToDataTable(this string json)
DataTable dataTable = new DataTable();
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxV //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize&ArrayList&(json);
if (arrayList.Count & 0)
foreach (Dictionary&string, object& dictionary in arrayList)
if (dictionary.Keys.Count&string&() == 0)
result = dataT
if (dataTable.Columns.Count == 0)
foreach (string current in dictionary.Keys)
dataTable.Columns.Add(current, dictionary[current].GetType());
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
dataRow[current] = dictionary[current];
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
result = dataT
#endregion
#region 转换为string字符串类型
/// &summary&
转换为string字符串类型
/// &/summary&
/// &param name="s"&获取需要转换的值&/param&
/// &param name="format"&需要格式化的位数&/param&
/// &returns&返回一个新的字符串&/returns&
public static string ToStr(this object s, string format = "")
string result = "";
if (format == "")
result = s.ToString();
result = string.Format("{0:" + format + "}", s);
#endregion
开源中国-程序员在线工具:
相关的代码(122)
2楼:TakeTry 发表于
3楼:ccflow 发表于
4楼:ccflow 发表于
我们用与ccflow上了&。
5楼:liuweihug 发表于
jquery插件-table转Json数据插件 - 前端编程 - IT工作生活这点事。Just Such So!
/UIweb/jquery-table-data-json.html
开源从代码分享开始
liangjw0504的其它代码3703人阅读
今天,老师讲了下json,虽然以前就知道这东西的用法了。不过,还是发现了自己对于json的应用了解得少了点。
Json可以被反序列化为Model、List&T&,DataTable,然后与相应的控件进行绑定(以前没想到这个用法,老注意ajax去了。可能这也是学生和工作的人的差别所在吧。经验!)
由于Json反序列化为Model、List&T&都比较容易,所以 这里就只记录反序列化为DataTable
/// &summary&
/// 扩展方法,将一个Json字符串反序列化为DataTable
/// &/summary&
/// &typeparam name=&T&&类型&/typeparam&
/// &param name=&str&&&/param&
/// &returns&DataTable&/returns&
public static DataTable DerializeToDataTable&T&(this string str)
DataTable dt = new DataTable();
if (str[0] == '[')//如果str的第一个字符是'[',则说明str里存放有多个model数据
//删除最后一个']'和第一个'[',顺序不能错。不然字符串的长度就不对了。
//因为每个model与model之间是用 &,&分隔的,所以改为用 &;&分隔
str = str.Remove(str.Length - 1, 1).Remove(0, 1).Replace(&},{&, &};{&);
JavaScriptSerializer js = new JavaScriptSerializer();
string[] items = str.Split(';');//用&;&分隔开多条数据
foreach (PropertyInfo property in typeof(T).GetProperties())//反射,获得T类型的所有属性
//创建一个新列,列名为属性名,类型为属性的类型。
DataColumn col = new DataColumn(property.Name, property.PropertyType);
dt.Columns.Add(col);
//循环,一个一个的反序列化
for (int i = 0; i & items.L i++)
//创建新行
DataRow dr = dt.NewRow();
//反序列化为一个T类型对象
T temp = js.Deserialize&T&(items[i]);
foreach (PropertyInfo property in typeof(T).GetProperties())
dr[property.Name] = property.GetValue(temp, null);
dt.Rows.Add(dr);
前台调用:
DataTable dt = TextBox1.Text.DerializeToDataTable&Goods&();Goods是我的一个Model。
这样,就可以将一个json字符串反序列化为DataTable了。
但是。需要特别注意的地方是。
DataColumn col = new DataColumn(property.Name, property.PropertyType);上面这一句,不能直接调用property.GetType()。如果调用了property.GetType()的话,则此时GetType()返回的实际是一个&RuntimePropertyInfo&的东西。
当然,非要调用GetType()也是可以的。那就需要先调用GetValue()。即,property.GetValue(....,....).GetType()。这样就可以了。
最后,该文章为学习笔记,如需转载请注明出处。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:74804次
排名:千里之外
原创:17篇
评论:20条
(2)(1)(4)(1)(3)(7)javascript - How to load json file to datatable - Stack Overflow
to customize your list.
Announcing Stack Overflow Documentation
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
I am very new in Jquery , I have a small task of loading json files to datatable using jquery ui dialog? , I am using visual studio 2010 to do this task
Here is the json codes:
"RELATIONMboSet":
{"rsStart":0,"rsCount":17,"RELATION":
[{"Attributes":{"RELATIONID":{"content":18,"resourceid":true},
"RELATIONNUM":{"content":"INCLUDES"},
"DESCRIPTION":{"content":"This is the container relationship for assets"},
"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},
"LINEAR":{"content":true},
"HASLD":{"content":false},
"IMPORTED":{"content":false},
"ISLINEARREF":{"content":false},
"USEWITH":{"content":"ASSET"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":17,"resourceid":true},
"RELATIONNUM":{"content":"CONNECTS"},"DESCRIPTION":{"content":"Source is connected to the Target"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"BIDIRECTIONAL"},"LINEAR":{"content":true},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"ASSET"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":2,"resourceid":true},
"RELATIONNUM":{"content":"INSTALON"},"DESCRIPTION":{"content":"Installed On"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},
"HASLD":{"content":false},"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},
"USEWITH":{"content":"CI"},"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":1,"resourceid":true},
"RELATIONNUM":{"content":"RUNSON"},"DESCRIPTION":{"content":"Runs On"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":3,"resourceid":true},
"RELATIONNUM":{"content":"CONTAINS"},"DESCRIPTION":{"content":"Contains"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},"HASLD":{"content":false},"IMPORTED":{"content":false},
"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":5,"resourceid":true},
"RELATIONNUM":{"content":"RELATES"},"DESCRIPTION":{"content":"Relates"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"BIDIRECTIONAL"},
"LINEAR":{"content":false},"HASLD":{"content":false},"IMPORTED":{"content":false},
"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":7,"resourceid":true},
"RELATIONNUM":{"content":"AFFECTS"},"DESCRIPTION":{"content":"Affects"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":4,"resourceid":true},
"RELATIONNUM":{"content":"MANAGES"},"DESCRIPTION":{"content":"Manages"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":6,"resourceid":true},
"RELATIONNUM":{"content":"FEDERATES"},"DESCRIPTION":{"content":"Federates"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":11,"resourceid":true},
"RELATIONNUM":{"content":"INTERSECTS"},"DESCRIPTION":{"content":"Intersects With"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"BIDIRECTIONAL"},"LINEAR":{"content":true},
"HASLD":{"content":false},"IMPORTED":{"content":false},"ISLINEARREF":{"content":true},
"USEWITH":{"content":"ASSET"},"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":14,"resourceid":true},
"RELATIONNUM":{"content":"BECOMES"},"DESCRIPTION":{"content":"One linear asset merges into another"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":true},
"HASLD":{"content":false},"IMPORTED":{"content":false},"ISLINEARREF":{"content":true},
"USEWITH":{"content":"ASSET"},"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":13,"resourceid":true},
"RELATIONNUM":{"content":"PARALLEL"},"DESCRIPTION":{"content":"One linear asset runs alongside another"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"BIDIRECTIONAL"},"LINEAR":{"content":true},
"HASLD":{"content":false},"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"ASSET"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":15,"resourceid":true},
"RELATIONNUM":{"content":"SPLITS FROM"},"DESCRIPTION":{"content":"One linear asset diverges from another"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":true},
"HASLD":{"content":false},"IMPORTED":{"content":false},"ISLINEARREF":{"content":true},
"USEWITH":{"content":"ASSET"},"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":16,"resourceid":true},
"RELATIONNUM":{"content":"CARRIES"},"DESCRIPTION":{"content":"A bridge carries a linear asset"},
"LANGCODE":{"content":"EN"},"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":true},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"ASSET"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":37,"resourceid":true},
"RELATIONNUM":{"content":"BACKSUP"},"DESCRIPTION":{"content":"Backs Up"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":true},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"ASSET"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":39,"resourceid":true},
"RELATIONNUM":{"content":"BACKED UP BY"},"DESCRIPTION":{"content":"Backed up By"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":true},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"ASSET"},
"ALLOWOVERRIDE":{"content":false}}},{"Attributes":{"RELATIONID":{"content":41,"resourceid":true},
"RELATIONNUM":{"content":"MIRROR"},"DESCRIPTION":{"content":"Mirror"},"LANGCODE":{"content":"EN"},
"TYPE":{"content":"UNIDIRECTIONAL"},"LINEAR":{"content":false},"HASLD":{"content":false},
"IMPORTED":{"content":false},"ISLINEARREF":{"content":false},"USEWITH":{"content":"CI"},
"ALLOWOVERRIDE":{"content":false}}}]}}
And using below jquery ui dialog :
&div id ='legend-dialog' &
&table id="legend_table" style="width:100%"&
&th&Relation Name&/th&
&th&Description&/th&
&th&Type&/th&
&th&Color&/th&
&td&INCLUDES&/td&
&td&This is the container relationship for assets.&/td&
&td&UNIDIRECTIONAL&/td&
&td&&div class="legencolor1" style='background-color:#888;'&&/div&&/td&
&td&RELATES&/td&
&td&Relates&/td&
&td&BIDIRECTIONAL&/td&
&td&&div class="legencolor2" style='background-color:#888;'&&/div&&/td&
&p style="margin-top:5px"&
&button type="button" class="btn btn-warning btn-xs" id="closebtn" style="float: margin-left:5px"&Close&/button&
&button type="button" class="btn btn-primary btn-xs" id="applybtn" style="float: margin-right:5px"&Apply&/button&
Any ideas on how I can go about this?
17.5k52349
This seems to work:
$(document).ready(function(){
var table = $('#example').DataTable({
"columns":[
"title":"Relation Name"
"title":"Description"
"title":"Type"
"title":"Color",
"render": function(){
return '&div class="legencolor1" style="background-color:#888;"&&&/div&';
$.each(data.RELATIONMboSet.RELATION, function(k, v){
table.row.add([
v.Attributes.RELATIONNUM.content,
v.Attributes.DESCRIPTION.content,
v.Attributes.TYPE.content
]).draw();
As long as your lump of data is assigned to a variable called data before the jQuery document ready function.
I tried using the data as a data source within the DataTable initialization script but it didn't like Attributes as a name... working JSFiddle:
Hope that helps.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledC# 怎么将json 转换成 datatable_百度知道

我要回帖

更多关于 wps小数点四舍五入 的文章

 

随机推荐