最近因为一直在看ext
所以和json 打交道打的比较多,
以前都是习惯性的
把数据读到dataset
之后调用ConvertToJson方法
(博客园有这个,只不过与ext的json稍微有点bug,更正在下面)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Reflection;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// ConvertToJson一个存储对象,收获Json
/// </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//非著乃编。。。。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class JsonConvert
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private static void WriteDataRow(StringBuilder sb, DataRow row)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("{");
foreach (DataColumn column in row.Table.Columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sb.AppendFormat("\"
{0}\":", column.ColumnName);
WriteValue(sb, row[column]);
sb.Append(",");
}
// Remove the trailing comma.
if (row.Table.Columns.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--sb.Length;
}
sb.Append("}");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void WriteDataSet(StringBuilder sb, DataSet ds)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("{\"Tables\":{");
foreach (DataTable table in ds.Tables)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sb.AppendFormat("\"
{0}\":", table.TableName);
WriteDataTable(sb, table);
sb.Append(",");
}
// Remove the trailing comma.
if (ds.Tables.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--sb.Length;
}
sb.Append("}}");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void WriteDataTable(StringBuilder sb, DataTable table)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("{\"rows\":[");
foreach (DataRow row in table.Rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteDataRow(sb, row);
sb.Append(",");
}
// Remove the trailing comma.
if (table.Rows.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--sb.Length;
}
sb.Append("]}");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void WriteEnumerable(StringBuilder sb, IEnumerable e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool hasItems = false;
sb.Append("[");
foreach (object val in e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteValue(sb, val);
sb.Append(",");
hasItems = true;
}
// Remove the trailing comma.
if (hasItems)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--sb.Length;
}
sb.Append("]");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void WriteHashtable(StringBuilder sb, Hashtable e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool hasItems = false;
sb.Append("{");
foreach (string key in e.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
sb.AppendFormat("\"
{0}\":", key.ToLower());
WriteValue(sb, e[key]);
sb.Append(",");
hasItems = true;
}
// Remove the trailing comma.
if (hasItems)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--sb.Length;
}
sb.Append("}");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void WriteObject(StringBuilder sb, object o)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
sb.Append("{");
bool hasMembers = false;
foreach (MemberInfo member in members)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool hasValue = false;
object val = null;
if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FieldInfo field = (FieldInfo)member;
val = field.GetValue(o);
hasValue = true;
}
else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PropertyInfo property = (PropertyInfo)member;
if (property.CanRead && property.GetIndexParameters().Length == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
val = property.GetValue(o, null);
hasValue = true;
}
}
if (hasValue)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("\"");
sb.Append(member.Name);
sb.Append("\":");
WriteValue(sb, val);
sb.Append(",");
hasMembers = true;
}
}
if (hasMembers)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
--sb.Length;
}
sb.Append("}");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static void WriteString(StringBuilder sb, string s)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("\"");
foreach (char c in s)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch (c)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case '\"':
sb.Append("\\\"");
break;
case '\\':
sb.Append("\\\\");
break;
case '\b':
sb.Append("\\b");
break;
case '\f':
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n");
break;
case '\r':
sb.Append("\\r");
break;
case '\t':
sb.Append("\\t");
break;
default:
int i = (int)c;
if (i < 32 || i > 127)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.AppendFormat("\\u{0:X04}", i);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append(c);
}
break;
}
}
sb.Append("\"");
}
public static void WriteValue(StringBuilder sb, object val)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (val == null || val == System.DBNull.Value)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("null");
}
else if (val is string || val is Guid)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteString(sb, val.ToString());
}
else if (val is bool)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append(val.ToString().ToLower());
}
else if (val is double ||
val is float ||
val is long ||
val is int ||
val is short ||
val is byte ||
val is decimal)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.AppendFormat(CultureInfo.InvariantCulture.NumberFormat, "{0}", val);
}
else if (val.GetType().IsEnum)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append((int)val);
}
else if (val is DateTime)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sb.Append("new Date(\"");
sb.Append(((DateTime)val).ToString("MMMM, d yyyy HH:mm:ss", new CultureInfo("en-US", false).DateTimeFormat));
sb.Append("\")");
}
else if (val is DataSet)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteDataSet(sb, val as DataSet);
}
else if (val is DataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteDataTable(sb, val as DataTable);
}
else if (val is DataRow)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteDataRow(sb, val as DataRow);
}
else if (val is Hashtable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteHashtable(sb, val as Hashtable);
}
else if (val is IEnumerable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteEnumerable(sb, val as IEnumerable);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
WriteObject(sb, val);
}
}
public static string ConvertToJson(object o)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringBuilder sb = new StringBuilder();
WriteValue(sb, o);
return sb.ToString();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
随着项目的深入,
我发现这么做好处很显然,速度快,但是坏处也有,东西一多,到处都是dataset。。。
看了<你必须知道的.net >后,
我就在想,是不是我们需要把这些封装到类里面来传呢?
比如说表
----------------------------------
table
customers
----------------------------------
id int (PK)
name nvarchar(10)
sex bit
phone nvarchar(15)
Email nvarchar(30)
. .
. .
. .
----------------------------------
中,我先定义个类,基本情况如下
—————————————————
Student
—————————————————
+Id :int
+Name:string
+Sex:bool
+Phone:string
+Email:string
—————————————————
changeName(string):void
.
.
.
—————————————————
之后实例化它,先将表中的信息填写到这个对象里面
之后通过Json.Net 中的
string json =
JavaScriptConvert.SerializeObject(student);
这样的话貌似每个都变成了对象方便处理,当然,也复杂不少。。。。
初学者,以往的blog 都不敢公开,
写到这个确实觉得确实有点意思,决定第一次放到新手区。。
希望得到各位高手指教,本人先在这里谢谢大家了。
code在下
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class Default2 : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//自己写的一个方法,实现的就是返回个dt,旧些时候的帖子上有这个的代码
SqlCon sqlCon = new SqlCon("test1ConnectionString");
DataTable dt = sqlCon.tableFromSql("select * from customers");
// 返回dt完毕
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//看看dt中有多少行
int count = dt.Rows.Count;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//申明一个对象数组
Customer[] customer = new Customer[count];
//初始化每个对象
for (int i = 0; i < count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
customer[i] = new Customer();
DataRow dr = dt.Rows[i];
customer[i].Id = int.Parse(dr["id"].ToString());
customer[i].Name = dr["Name"].ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string a = JavaScriptConvert.SerializeObject(customer);
Response.Write(a);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*result here
* [
* {"Id":1,"Name":"赵一","Sex":"Female","Email":null},
* {"Id":2,"Name":"钱二","Sex":"Female","Email":null},
* {"Id":3,"Name":"孙三","Sex":"Female","Email":null},
* {"Id":4,"Name":"李四","Sex":"Female","Email":null}
* ]
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
class Customer
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private int _id;
private string _name;
private bool _sex;
private string _email;
public int Id
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{_id = value; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _id; }
}
public string Name
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _name = value.ToUpper(); }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
}
public string Sex
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (value.ToUpper() == "True")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_sex = true;
}
else if (value.ToUpper() == "False")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_sex = false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("input male or female only");
}
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_sex == true)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ return "Male"; }
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ return "Female"; }
}
}
public string Email
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(Email.Contains("@")==false)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("input Wrong Email");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{_email = value;}
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return _email;
}
}
public Customer()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//do Nothing
}
public Customer(int id,string name,string sex,string email)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Id = id;
this.Name = name;
this.Sex = sex;
this.Email = email;
}
public void eating()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Response.Write("cry");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)