1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; using System.Data;
using System.Collections;
using System.Web.Script.Serialization; /// <summary>
///JsonHelper 的摘要说明
/// </summary>
public class JsonHelper
{
public JsonHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
} #region Json 与 DataTable 的相互转换 /// <summary>
/// Json 字符串转换为DataTable数据集合
/// </summary>
/// <param name="strJson">待转换的Json字符串</param>
/// <returns>返回DataTable集合</returns>
public static DataTable JsonToDataTable(string strJson)
{
DataTable dt = new DataTable(); //实例化
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值 ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(strJson);
if (arrayList.Count > )
{
foreach (Dictionary<string, object> dic in arrayList)
{
if (dic.Keys.Count<string>() == )
{
return dt;
}
if (dt.Columns.Count == )
{
foreach (string key in dic.Keys)
{
dt.Columns.Add(key, dic[key].GetType());
}
}
DataRow dataRow = dt.NewRow();
foreach (string key in dic.Keys)
{
dataRow[key] = dic[key];
} dt.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
return dt;
} /// <summary>
/// DataTable 对象 转换为Json 字符串
/// </summary>
/// <param name="dt">带转换的DataTable数据集合</param>
/// <returns>返回Json字符串</returns>
public static string DataTableToJson(DataTable dt)
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值 ArrayList arrayList = new ArrayList();
foreach (DataRow dataRow in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>(); //实例化一个参数集合
foreach (DataColumn dataColumn in dt.Columns)
{
dic.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
arrayList.Add(dic); //ArrayList集合中添加键值
} return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串
} #endregion Json 与 DataTable 的相互转换 }

2

CollatingOfData 之 JsonHelper的更多相关文章

  1. JSON扩展类——JsonHelper

    1.引用Newtonsoft.Json库(JSON.NET). 2.复制粘贴JsonHelper吧. 源代码: using System; using System.Collections.Gener ...

  2. JsonHelper MergeJsonTemplate

    namespace Test { using Newtonsoft.Json; using System; using System.Collections.Generic; using System ...

  3. JsonHelper developed by using Newtonsoft.Json.NET, Deserialize to <T> object , XmlToJson/JsonToXml, QuoteName by using JToken Path.

    namespace TestConsoleApplication { using System; using System.Diagnostics; using System.Threading; u ...

  4. 【C#公共帮助类】JsonHelper 操作帮助类, 以后再也不用满地找Json了,拿来直接用

     四个主要操作类:JsonConverter .JsonHelper .JsonSplit .AjaxResult 一.JsonConverter: 自定义查询对象转换动态类.object动态类转换j ...

  5. C#序列化及反序列化Json对象通用类JsonHelper

    当今的程序界Json大行其道.因为Json对象具有简短高效等优势,广受广大C#码农喜爱.这里发一个序列化及反序列化Json对象通用类库,希望对大家有用. public class JsonHelper ...

  6. asp.net的JSONHelper 类

    调用方法: ){    jsons = json.ToString();}else{    jsons = @"{success:false}";}return jsons; JS ...

  7. C# jsonhelper

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Sc ...

  8. JsonHelper

    .net下的json序列化在以前没有Newtonsoft.Json崭露头角之前采用System.Web.Script.Serialization命名空间下的JavaScriptSerializer对象 ...

  9. JsonHelper类(序列化和反序列化辅助类)

       1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; ...

随机推荐

  1. 修改MongoDb的 DB 和 Log 存储路径 (Windows)-摘自网络

    Create a file called mongod.cfg in MongoDB folder if you dont have it. In my case: C:\Users\ivanbtru ...

  2. redis的string类型

    string : string类型是二进制安全的, 可以包含任何数据,比如jpg图片或者序列化的对象 . 方法 : set : 设置key对应的值为string类型的value set  name   ...

  3. 在Windows Server 下安装 Oracle 11G 的一般步骤

  4. 【转】Maven实战(一)---Maven Build--缺少Jar包

    原博文出于: http://blog.csdn.net/liutengteng130/article/details/41426955   感谢! 新建的Maven项目,在build的时候总是打包失败 ...

  5. 为Delphi程序增加UAC功能(管理员身份运行exe)

    相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...

  6. (转)UML常用图的几种关系的总结

    在UML的类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realization), 关联(Association), 聚合(Aggregation), 组合(Comp ...

  7. mybatis中为sql中传值#{}和${}的区别

    在mybatis中,配置文件中sql的值,用#{}表示,例如: <select id="getTeacher" resultType="Teacher"& ...

  8. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  9. 浅析网站开发中的 meta 标签的作用

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  10. 绑定线程到特定CPU处理器

    参考这篇文章 http://blog.chinaunix.net/uid-27761170-id-5050258.html 代码如下: #define _GNU_SOURCE #include < ...