日期:2014-05-17 浏览次数:20536 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
/// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1
{
public Class1()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static string objectOfJson<T>(List<T> list)
{
JavaScriptSerializer json = new JavaScriptSerializer();
//将list集合解析为json字符串
string strJson = json.Serialize(list);
return strJson;
}
/// <summary>
/// 反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strJson"></param>
/// <returns></returns>
public static List<T> JsonOfobject<T>(string strJson)
{
if (string.IsNullOrEmpty(strJson))
{
strJson = "[]";
}
JavaScriptSerializer json = new JavaScriptSerializer();
//将json字符串解析为一个对象集合
return json.Deserialize(strJson, typeof(List<T>)) as List<T>;
}
}