C# jsonhelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data; /// <summary>
/// JSON帮助类
/// </summary>
publicclassJSONHelper
{
/// <summary>
/// 对象转JSON
/// </summary>
/// <param name="obj">对象</param>
/// <returns>JSON格式的字符串</returns>
publicstaticstringObjectToJSON(object obj)
{
JavaScriptSerializer jss =newJavaScriptSerializer();
try
{
return jss.Serialize(obj);
}
catch(Exception ex)
{ thrownewException("JSONHelper.ObjectToJSON(): "+ ex.Message);
}
} /// <summary>
/// 数据表转键值对集合 www.2cto.com
/// 把DataTable转成 List集合, 存每一行
/// 集合中放的是键值对字典,存每一列
/// </summary>
/// <param name="dt">数据表</param>
/// <returns>哈希表数组</returns>
publicstaticList<Dictionary<string,object>>DataTableToList(DataTable dt)
{
List<Dictionary<string,object>> list
=newList<Dictionary<string,object>>(); foreach(DataRow dr in dt.Rows)
{
Dictionary<string,object> dic =newDictionary<string,object>();
foreach(DataColumn dc in dt.Columns)
{
dic.Add(dc.ColumnName, dr[dc.ColumnName]);
}
list.Add(dic);
}
return list;
} /// <summary>
/// 数据集转键值对数组字典
/// </summary>
/// <param name="dataSet">数据集</param>
/// <returns>键值对数组字典</returns>
publicstaticDictionary<string,List<Dictionary<string,object>>>DataSetToDic(DataSet ds)
{
Dictionary<string,List<Dictionary<string,object>>> result =newDictionary<string,List<Dictionary<string,object>>>(); foreach(DataTable dt in ds.Tables)
result.Add(dt.TableName,DataTableToList(dt)); return result;
} /// <summary>
/// 数据表转JSON
/// </summary>
/// <param name="dataTable">数据表</param>
/// <returns>JSON字符串</returns>
publicstaticstringDataTableToJSON(DataTable dt)
{
returnObjectToJSON(DataTableToList(dt));
} /// <summary>
/// JSON文本转对象,泛型方法
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="jsonText">JSON文本</param>
/// <returns>指定类型的对象</returns>
publicstatic T JSONToObject<T>(string jsonText)
{
JavaScriptSerializer jss =newJavaScriptSerializer();
try
{
return jss.Deserialize<T>(jsonText);
}
catch(Exception ex)
{
thrownewException("JSONHelper.JSONToObject(): "+ ex.Message);
}
} /// <summary>
/// 将JSON文本转换为数据表数据
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>数据表字典</returns>
publicstaticDictionary<string,List<Dictionary<string,object>>>TablesDataFromJSON(string jsonText)
{
returnJSONToObject<Dictionary<string,List<Dictionary<string,object>>>>(jsonText);
} /// <summary>
/// 将JSON文本转换成数据行
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>数据行的字典</returns>
publicstaticDictionary<string,object>DataRowFromJSON(string jsonText)
{
returnJSONToObject<Dictionary<string,object>>(jsonText);
}
}
C# jsonhelper的更多相关文章
- JSON扩展类——JsonHelper
1.引用Newtonsoft.Json库(JSON.NET). 2.复制粘贴JsonHelper吧. 源代码: using System; using System.Collections.Gener ...
- JsonHelper MergeJsonTemplate
namespace Test { using Newtonsoft.Json; using System; using System.Collections.Generic; using System ...
- 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 ...
- 【C#公共帮助类】JsonHelper 操作帮助类, 以后再也不用满地找Json了,拿来直接用
四个主要操作类:JsonConverter .JsonHelper .JsonSplit .AjaxResult 一.JsonConverter: 自定义查询对象转换动态类.object动态类转换j ...
- C#序列化及反序列化Json对象通用类JsonHelper
当今的程序界Json大行其道.因为Json对象具有简短高效等优势,广受广大C#码农喜爱.这里发一个序列化及反序列化Json对象通用类库,希望对大家有用. public class JsonHelper ...
- asp.net的JSONHelper 类
调用方法: ){ jsons = json.ToString();}else{ jsons = @"{success:false}";}return jsons; JS ...
- JsonHelper
.net下的json序列化在以前没有Newtonsoft.Json崭露头角之前采用System.Web.Script.Serialization命名空间下的JavaScriptSerializer对象 ...
- JsonHelper类(序列化和反序列化辅助类)
1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Web; ...
- CollatingOfData 之 JsonHelper
1 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System. ...
随机推荐
- ulimit命令
原文链接 linux下默认是不产生core文件的,要用ulimit -c unlimited放开 概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件 ...
- Spring Data JPA进阶——Specifications和Querydsl
Spring Data JPA进阶--Specifications和Querydsl 本篇介绍一下spring Data JPA中能为数据访问程序的开发带来更多便利的特性,我们知道,Spring Da ...
- Myeclipse8.5 反编译插件 jad 安装
准备工作 下载jad.exe文件和下载jadeclipse插件:http://pan.baidu.com/s/1pJKjVwn JadClipse 官网:http://jadclipse.source ...
- PL/Proxy介绍
PL/Proxy 介绍 一.概述 1.PL/Proxy 是一个采用PL Language语言的数据库分区系统. 目的:轻松访问分区数据库 它的理念是代理远程函数体内指定.函数调用同样标签创建的函数,所 ...
- unbutu下搭建FTP服务
安装 apt-get install vsftpd 启动 service vsftpd start 第一次连接的时候出了点问题,报了一个 login incorrect 530的连接错误 然后百度了一 ...
- 【leetcode】 Search a 2D Matrix (easy)
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Jquery选中行实现行中的Checkbox的选中与取消选中
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- Git的一些实用操作
Ref:http://stackoverflow.com/questions/17195861/undo-git-update-index-assume-unchanged-file 1. 添加本地忽 ...
- hdu2030 汉字统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2030 解题思路:主要考察汉字的编码方式, 汉字机内码在计算机的表达方式的描述是,使用二个字节,汉字的每 ...
- HTML5 – 3.加强版ol
<ol> 标签定义了一个有序列表. 列表排序以数字来显示. 使用<li> 标签来定义列表选项. 提示和注释 提示: 如果需要无序列表,请使用 <ul> 标签. 提示 ...