引用 Newtonsoft.Json

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Script.Serialization; //下载Newtonsoft.Json.dll
public class JsonOperate
{
private JavaScriptSerializer Jss = new JavaScriptSerializer(); private string _IdField; private string _IdType; private string _TextField; private string _StateField; private string _CheckedField; private string _ParentIdField; private string _IconClsField; private string _AttributesField; private string _IconCls; private string _ParentId; private string _State; private string _AllData; private string _Checked; private string _Attributes; private DataTable _JsonTable; public DataTable JsonTable
{
get
{
return this._JsonTable;
}
set
{
this._JsonTable = value;
}
} public string IdField
{
get
{
return this._IdField;
}
set
{
this._IdField = value;
}
} public string IdType
{
get
{
return this._IdType;
}
set
{
this._IdType = value;
}
} public string TextField
{
get
{
return this._TextField;
}
set
{
this._TextField = value;
}
} public string StateField
{
get
{
return this._StateField;
}
set
{
this._StateField = value;
}
} public string CheckedField
{
get
{
return this._CheckedField;
}
set
{
this._CheckedField = value;
}
} public string SelectedField
{
get;
set;
} public string ParentId
{
get
{
return this._ParentId;
}
set
{
this._ParentId = value;
}
} public string ParentIdField
{
get
{
return this._ParentIdField;
}
set
{
this._ParentIdField = value;
}
} public string AttributesField
{
get
{
return this._AttributesField;
}
set
{
this._AttributesField = value;
}
} public string IconClsField
{
get
{
return this._IconClsField;
}
set
{
this._IconClsField = value;
}
} public string IconCls
{
get
{
return this._IconCls;
}
set
{
this._IconCls = value;
}
} public string Attributes
{
get
{
return this._Attributes;
}
set
{
this._Attributes = value;
}
} public string Checked
{
get
{
return this._Checked;
}
set
{
this._Checked = value;
}
} public string State
{
get
{
return this._State;
}
set
{
this._State = value;
}
} public string AllData
{
get
{
return this._AllData;
}
set
{
this._AllData = value;
}
} public string GetTreeJson()
{
System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
string text = "";
if (!string.IsNullOrEmpty(this.ParentIdField))
{
text = this.ParentIdField + " is Null";
if (string.IsNullOrEmpty(this.IdType))
{
text = text + " or " + this.ParentIdField + "='0'";
}
else
{
text = text + " or " + this.ParentIdField + "=''";
}
}
if (this.AllData == "true")
{
list.Add(new System.Collections.Generic.Dictionary<string, object>
{
{
"id",
""
},
{
"text",
"全部"
}
});
}
DataRow[] array = this.JsonTable.Select(text);
for (int i = ; i < array.Length; i++)
{
DataRow dataRow = array[i];
System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
this.ParentId = dataRow[this.IdField].ToString();
dictionary.Add("id", dataRow[this.IdField].ToString());
dictionary.Add("text", dataRow[this.TextField].ToString());
if (!string.IsNullOrEmpty(this.StateField))
{
dictionary.Add("state", dataRow[this.StateField].ToString());
}
else if (!string.IsNullOrEmpty(this.State))
{
dictionary.Add("state", this.State);
}
if (!string.IsNullOrEmpty(this.CheckedField))
{
dictionary.Add("checked", dataRow[this.CheckedField].ToString());
}
else if (!string.IsNullOrEmpty(this.Checked))
{
dictionary.Add("checked", this.Checked);
}
if (!string.IsNullOrEmpty(this.SelectedField))
{
dictionary.Add("selected", dataRow[this.SelectedField].ToString());
}
if (!string.IsNullOrEmpty(this.IconClsField))
{
dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
}
else if (!string.IsNullOrEmpty(this.IconCls))
{
dictionary.Add("iconCls", this.IconCls);
}
if (!string.IsNullOrEmpty(this.AttributesField))
{
dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
}
else if (!string.IsNullOrEmpty(this.Attributes))
{
dictionary.Add("attributes", this.Attributes);
}
if (!string.IsNullOrEmpty(this.ParentIdField))
{
dictionary.Add("children", this.GetTreeChildrenJson());
}
list.Add(dictionary);
}
return this.Jss.Serialize(list);
} public System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> GetTreeChildrenJson()
{
System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
string filterExpression = this.ParentIdField + " = '" + this.ParentId + "'";
DataRow[] array = this.JsonTable.Select(filterExpression);
for (int i = ; i < array.Length; i++)
{
DataRow dataRow = array[i];
System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
this.ParentId = dataRow[this.IdField].ToString();
dictionary.Add("id", dataRow[this.IdField].ToString());
dictionary.Add("text", dataRow[this.TextField].ToString());
if (!string.IsNullOrEmpty(this.StateField))
{
dictionary.Add("state", dataRow[this.StateField].ToString());
}
else if (!string.IsNullOrEmpty(this.State))
{
dictionary.Add("state", this.State);
}
if (!string.IsNullOrEmpty(this.CheckedField))
{
dictionary.Add("checked", dataRow[this.CheckedField].ToString());
}
else if (!string.IsNullOrEmpty(this.Checked))
{
dictionary.Add("checked", this.Checked);
}
if (!string.IsNullOrEmpty(this.SelectedField))
{
dictionary.Add("selected", dataRow[this.SelectedField].ToString());
}
if (!string.IsNullOrEmpty(this.IconClsField))
{
dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
}
else if (!string.IsNullOrEmpty(this.IconCls))
{
dictionary.Add("iconCls", this.IconCls);
}
if (!string.IsNullOrEmpty(this.AttributesField))
{
dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
}
else if (!string.IsNullOrEmpty(this.Attributes))
{
dictionary.Add("attributes", this.Attributes);
}
dictionary.Add("children", this.GetTreeChildrenJson());
list.Add(dictionary);
}
return list;
} public string GetTreeGridJson()
{
System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
string text = this.ParentIdField + " is Null";
if (string.IsNullOrEmpty(this.IdType))
{
text = text + " or " + this.ParentIdField + "='0'";
}
else
{
text = text + " or " + this.ParentIdField + "=''";
}
DataRow[] array = this.JsonTable.Select(text);
for (int i = ; i < array.Length; i++)
{
DataRow dataRow = array[i];
System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
foreach (DataColumn dataColumn in this.JsonTable.Columns)
{
if (dataColumn.DataType == System.Type.GetType("System.DateTime"))
{
string value;
if (string.IsNullOrEmpty(dataRow[dataColumn.ColumnName].ToString()))
{
value = "";
}
else
{
value = System.Convert.ToDateTime(dataRow[dataColumn.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm");
}
dictionary.Add(dataColumn.ColumnName, value);
}
else
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
}
this.ParentId = dataRow[this.IdField].ToString();
if (!string.IsNullOrEmpty(this.StateField))
{
dictionary.Add("state", dataRow[this.StateField].ToString());
}
else if (!string.IsNullOrEmpty(this.State))
{
dictionary.Add("state", this.State);
}
if (!string.IsNullOrEmpty(this.CheckedField))
{
dictionary.Add("checked", dataRow[this.CheckedField].ToString());
}
else if (!string.IsNullOrEmpty(this.Checked))
{
dictionary.Add("checked", this.Checked);
}
if (!string.IsNullOrEmpty(this.IconClsField))
{
dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
}
else if (!string.IsNullOrEmpty(this.IconCls))
{
dictionary.Add("iconCls", this.IconCls);
}
if (!string.IsNullOrEmpty(this.AttributesField))
{
dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
}
else if (!string.IsNullOrEmpty(this.Attributes))
{
dictionary.Add("attributes", this.Attributes);
}
dictionary.Add("children", this.GetTreeGridChildrenJson());
list.Add(dictionary);
}
return this.Jss.Serialize(list);
} public System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> GetTreeGridChildrenJson()
{
System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
string filterExpression = this.ParentIdField + " = '" + this.ParentId + "'";
DataRow[] array = this.JsonTable.Select(filterExpression);
for (int i = ; i < array.Length; i++)
{
DataRow dataRow = array[i];
System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
foreach (DataColumn dataColumn in this.JsonTable.Columns)
{
if (dataColumn.DataType == System.Type.GetType("System.DateTime"))
{
string value;
if (string.IsNullOrEmpty(dataRow[dataColumn.ColumnName].ToString()))
{
value = "";
}
else
{
value = System.Convert.ToDateTime(dataRow[dataColumn.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm");
}
dictionary.Add(dataColumn.ColumnName, value);
}
else
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
}
this.ParentId = dataRow[this.IdField].ToString();
if (!string.IsNullOrEmpty(this.StateField))
{
dictionary.Add("state", dataRow[this.StateField].ToString());
}
else if (!string.IsNullOrEmpty(this.State))
{
dictionary.Add("state", this.State);
}
if (!string.IsNullOrEmpty(this.CheckedField))
{
dictionary.Add("checked", dataRow[this.CheckedField].ToString());
}
else if (!string.IsNullOrEmpty(this.Checked))
{
dictionary.Add("checked", this.Checked);
}
if (!string.IsNullOrEmpty(this.IconClsField))
{
dictionary.Add("iconCls", dataRow[this.IconClsField].ToString());
}
else if (!string.IsNullOrEmpty(this.IconCls))
{
dictionary.Add("iconCls", this.IconCls);
}
if (!string.IsNullOrEmpty(this.AttributesField))
{
dictionary.Add("attributes", dataRow[this.AttributesField].ToString());
}
else if (!string.IsNullOrEmpty(this.Attributes))
{
dictionary.Add("attributes", this.Attributes);
}
dictionary.Add("children", this.GetTreeGridChildrenJson());
list.Add(dictionary);
}
return list;
} public string GetDataGridJson(DataTable JsonTable)
{
return string.Concat(new string[]
{
"{ \"total\":",
JsonTable.Rows.Count.ToString(),
",\"rows\":",
this.TableToBasicJson(JsonTable),
"}"
});
} public string GetDataGridJson(DataTable JsonTable, string strCount)
{
return string.Concat(new string[]
{
"{ \"total\":",
strCount,
",\"rows\":",
this.TableToBasicJson(JsonTable),
"}"
});
} public string GetDataGridJson(DataTable JsonTable, int count)
{
return string.Concat(new object[]
{
"{ \"total\":",
count,
",\"rows\":",
this.TableToBasicJson(JsonTable),
"}"
});
} public string GetDataGridJson(DataTable JsonTable, DataTable strCount, DataTable FooterTable)
{
return string.Concat(new object[]
{
"{ \"total\":",
strCount,
",\"rows\":",
this.TableToBasicJson(JsonTable),
",\"footer\":",
this.TableToBasicJson(FooterTable),
"}"
});
} public string TableToBasicJson(DataTable JsonTable)
{
System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
foreach (DataRow dataRow in JsonTable.Rows)
{
System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
foreach (DataColumn dataColumn in JsonTable.Columns)
{
if (dataColumn.DataType == System.Type.GetType("System.DateTime"))
{
string value;
if (string.IsNullOrEmpty(dataRow[dataColumn.ColumnName].ToString()))
{
value = "";
}
else
{
value = System.Convert.ToDateTime(dataRow[dataColumn.ColumnName].ToString()).ToString("yyyy-MM-dd HH:mm");
}
dictionary.Add(dataColumn.ColumnName, value);
}
else
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
}
}
list.Add(dictionary);
}
return this.Jss.Serialize(list);
} public DataTable JsonToTable(string JsonString)
{
System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>> list = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>();
list = this.Jss.Deserialize<System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, object>>>(JsonString);
DataTable dataTable = new DataTable();
if (list.Count > )
{
foreach (System.Collections.Generic.Dictionary<string, object> current in list)
{
if (dataTable.Columns.Count == )
{
foreach (string current2 in current.Keys)
{
dataTable.Columns.Add(current2, current[current2].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current2 in current.Keys)
{
dataRow[current2] = current[current2];
}
dataTable.Rows.Add(dataRow);
}
}
return dataTable;
} //public static string ToJson(object t)
//{
// Formatting arg_12_1 = 1;
// JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
// jsonSerializerSettings.set_NullValueHandling(0);
// return JsonConvert.SerializeObject(t, arg_12_1, jsonSerializerSettings);
//} //public static string ToJson(object t, bool HasNullIgnore)
//{
// string result;
// if (HasNullIgnore)
// {
// Formatting arg_1A_1 = 1;
// JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
// jsonSerializerSettings.set_NullValueHandling(1);
// result = JsonConvert.SerializeObject(t, arg_1A_1, jsonSerializerSettings);
// }
// else
// {
// result = JsonOperate.ToJson(t);
// }
// return result;
//} public static T FromJson<T>(string strJson) where T : class
{
T result;
if (!string.IsNullOrEmpty(strJson))
{
result = JsonConvert.DeserializeObject<T>(strJson);
}
else
{
result = default(T);
}
return result;
}
}

JsonOperate 帮助类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. C++ 可配置的类工厂

    项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...

  3. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  4. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  5. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  6. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  7. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  8. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  9. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

随机推荐

  1. DOM元素的大小和位置

    HTML: <div id="parent"> <div id="box"> 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测 ...

  2. Javascript初学篇章_1(概念/数据类型)

    Javascript是一门脚本语言,主要由浏览器来执行.它可以说是页面的灵魂,让页面活过来.与之前学的HTML5+CSS样式的不同之处就在于,JS能让静态网页成为一个动态网页,实现与用户的互动. Ja ...

  3. SQLite的原子提交原理

    本文描述了sqlite为保证数据库文件不被损坏而采取的种种手段.. 以下是原译者的摘要:http://www.kuqin.com/shuoit/20150618/346693.html 摘要: 本文源 ...

  4. php编译内容

    ./configure --prefix=/usrc/share/php--with-apxs2=/usr/share/apache2/bin/apxs--with-config-file-path= ...

  5. Angular2对待一个URL的处理流程

    直到一年前我还不会想到我会写一本书出来,直到两三年前我还不会想到会把自己的业余时间放在写作(技术博客)上.当初在机缘巧合之下,我只是想把自己工作中遇到的问题总结起来,发布在博客中以供日后查阅.不想竟然 ...

  6. 【RabbitMQ】 WorkQueues

    消息分发 在[RabbitMQ] HelloWorld中我们写了发送/接收消息的程序.这次我们将创建一个Work Queue用来在多个消费者之间分配耗时任务. Work Queues(又称为:Task ...

  7. java jdbc

    1.java 的jdbc类包括,DriverManager,Connection Connection con = DriverManager.getConnection("jdbc:odb ...

  8. 20145220&20145209&20145309信息安全系统设计基础实验报告(5)

    20145220&20145209&20145309信息安全系统设计基础实验报告(5) 实验报告链接: http://www.cnblogs.com/zym0728/p/6132249 ...

  9. Tomcat 随机挂掉

    最近遇到一些诡异的事情,1Apache + 4Tomcat的系统中,每天偶尔会发现其中的一台或几台Tomcat宕机了. 今天特别频繁,宕了有6次之多.    环境: Windows Server 20 ...

  10. 一步一步来做WebQQ机器人-(五)(发送消息||完结)

    × 本篇主要是: 发送QQ消息(to:好友,群),以及对小黄鸡抓包利用它的语言库 本文是WebQQ流程的最后一章 最后一章内容不多但我还是啰嗦,可能对大部分人都已知晓的流程方法我也会介绍一下 前面几个 ...