1、新建一个 JSON 类,实现如下所示代码:
/*
*
* 表示层的辅助类
*
* 功能:JSON序列化和反序列化
* 作者:凌霜残血
*
*/
public class JSON
{
public static string DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss";
public static string Encode(object o)
{
if (o == null || o.ToString() == "null")
return null;
if (o != null && (o.GetType() == typeof(String) || o.GetType() == typeof(string)))
{
return o.ToString();
}
//不随机排序的Hashtable
if (o.GetType() == typeof(NoSortHashtable))
{
NoSortHashtable ht = (NoSortHashtable)o;
string json = "{";
foreach (string key in ht.Keys)
{
object value = ht[key];
if (value.GetType() == typeof(String) || value.GetType() == typeof(string))
json += "\"" + key + "\":\"" + value.ToString() + "\","; //字符串
else
json += "\"" + key + "\":" + value.ToString().ToLower() + ","; //非字符串,如整型或布尔型
}
json = json.Substring(0, json.Length - 1); //去掉最后一个逗号
json += "}";
return json;
}
IsoDateTimeConverter dt = new IsoDateTimeConverter();
dt.DateTimeFormat = DateTimeFormat;
return JsonConvert.SerializeObject(o, dt);
}
public static object Decode(string json)
{
if (String.IsNullOrEmpty(json)) return "";
object o = JsonConvert.DeserializeObject(json);
if (o.GetType() == typeof(String) || o.GetType() == typeof(string))
{
o = JsonConvert.DeserializeObject(o.ToString());
}
object v = toObject(o);
return v;
}
public static object Decode(string json, Type type)
{
return JsonConvert.DeserializeObject(json, type);
}
private static object toObject(object o)
{
if (o == null) return null;
if (o.GetType() == typeof(string))
{
//判断是否符合2010-09-02T10:00:00的格式
string s = o.ToString();
if (s.Length == 19 && s[10] == 'T' && s[4] == '-' && s[13] == ':')
{
o = System.Convert.ToDateTime(o);
}
}
else if (o is JObject)
{
JObject jo = o as JObject;
Hashtable h = new Hashtable();
foreach (KeyValuePair<string, JToken> entry in jo)
{
h[entry.Key] = toObject(entry.Value);
}
o = h;
}
else if (o is IList)
{
ArrayList list = new ArrayList();
list.AddRange((o as IList));
int i = 0, l = list.Count;
for (; i < l; i++)
{
list[i] = toObject(list[i]);
}
o = list;
}
else if (typeof(JValue) == o.GetType())
{
JValue v = (JValue)o;
o = toObject(v.Value);
}
else
{
}
return o;
}
}
2、实现不随机排序的 Hashtable,新建 NoSortHashtable.cs 类,实现代码如下:
/*
*
* 表示层的辅助类
*
* 功能:实现Hashtable不随机进行排序
* 作者:王武
*
*/
public class NoSortHashtable : Hashtable
{
private ArrayList keys = new ArrayList(); //借助ArrayList来指定顺序,从而避免使用Hashtable原有的随机顺序
//构造函数
public NoSortHashtable()
{
}
//添加元素
public override void Add(object key, object value)
{
base.Add(key, value);
keys.Add(key);
}
//泛型的key集合
public override ICollection Keys
{
get
{
return keys;
}
}
//清除所有元素
public override void Clear()
{
base.Clear();
keys.Clear();
}
//删除一个元素
public override void Remove(object key)
{
base.Remove(key);
keys.Remove(key);
}
public override IDictionaryEnumerator GetEnumerator()
{
return base.GetEnumerator();
}
}
如此,可以将任何类型数据转化成Json字符串;注意两个类的引用:
那么 如何使用这个JSON类呢,如下所示:
protected void Page_Load(object sender, EventArgs e)
{
DataTable tbl = new DataTable();
tbl.Columns.Add("id1", typeof(Int32)); //Id
tbl.Columns.Add("text1", typeof(String)); //名称
tbl.Columns.Add("seq1", typeof(String)); //编号
List<Models.AlarmConfig> models = new List<Models.AlarmConfig>();
Models.AlarmConfig m1 = new Models.AlarmConfig();
m1.AlarmTypeId = 1;
m1.AlarmTypeName = "张三";
Models.AlarmConfig m2 = new Models.AlarmConfig();
m2.AlarmTypeId = 2;
m2.AlarmTypeName = "李四";
Models.AlarmConfig m3 = new Models.AlarmConfig();
m3.AlarmTypeId = 3;
m3.AlarmTypeName = "王者";
models.Add(m1);
models.Add(m2);
models.Add(m3);
foreach (Models.AlarmConfig model in models)
{
DataRow row = tbl.NewRow();
row["id"] = model.AlarmTypeId;
row["text"] = model.AlarmTypeName;
tbl.Rows.Add(row);
}
string json = JSON.Encode(tbl);
Response.Write(json);
}
这样只要在前端做一个Json字符串接收就可以了,运行效果如下:
Dll 文件下载:密码:uvwr
将Dll文件拷贝到bin文件夹下,添加外部引用即可;