补充知识:
1.字符串转比特数组
(1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串");
(2)byte[] bt=Convert.FromBase64String("字符串");
2.字符串转流
(1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes("字符串"));
(2)MemoryStream ms=new MemoryStream(Convert.FromBase64String("字符串"));
3.流转比特数组
(1)byte[] bt=ms.ToArray();
(2)MemoryStream ms=new MemoryStream();ms.Write(bt,0,ms.Length);
4.流转字符串
(1)string str=Convert.ToBase64String(ms.ToArray());
(2)string str=System.Text.Encoding.Default.GetString(ms.ToArray());
5.比特数组转字符串
(1)string str=System.Text.Encoding.Default.GetString(bt);
(2)string str=Convert.ToBase64String(bt);
6.比特数组转流
(1)MemoryStream ms=new MemoryStream(bt);
(2)MemoryStream ms=new MemoryStream();ms.Read(bt,0,bt.Lenght);
1. 添加引用System.ServiceModel.Web
2. 创建类
- public class JsonHelper
- {
- // 序列化
- public static string JsonSerializer<T>(T t)
- {
- // 使用 DataContractJsonSerializer 将 T 对象序列化为内存流。
- DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof (T));
- MemoryStream ms = new MemoryStream();
- // 使用 WriteObject 方法将 JSON 数据写入到流中。
- jsonSerializer.WriteObject(ms, t);
- // 流转字符串
- string jsonString = Encoding.UTF8.GetString(ms.ToArray());
- ms.Close();
- //替换Json的Date字符串
- string p = @"\\/Date\((\d+)\+\d+\)\\/";
- MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
- Regex reg = new Regex(p);
- jsonString = reg.Replace(jsonString, matchEvaluator);
- return jsonString;
- }
- public static T JsonDeserialize<T>(string jsonString)
- {
- //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式
- string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
- MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
- Regex reg = new Regex(p);
- jsonString = reg.Replace(jsonString, matchEvaluator);
- DataContractJsonSerializer jsonSerializer=new DataContractJsonSerializer(typeof(T));
- // 字符串转流
- MemoryStream ms=new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
- // 通过使用 DataContractJsonSerializer 的 ReadObject 方法,将 JSON 编码数据反序列化为T
- T obj = (T) jsonSerializer.ReadObject(ms);
- return obj;
- }
- public static string ConvertJsonDateToDateString(Match match)
- {
- string result = string.Empty;
- DateTime dateTime=new DateTime(1970,1,1);
- dateTime = dateTime.AddMilliseconds(long.Parse(match.Groups[1].Value));
- dateTime = dateTime.ToLocalTime();
- result = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
- return result;
- }
- private static string ConvertDateStringToJsonDate(Match m)
- {
- string result = string.Empty;
- DateTime dt = DateTime.Parse(m.Groups[0].Value);
- dt = dt.ToUniversalTime();
- TimeSpan ts = dt - DateTime.Parse("1970-01-01");
- result = string.Format("\\/Date({0}+0800)\\/",ts.TotalMilliseconds);
- return result;
- }
- }
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public DateTime dateTime { get; set; }
- }
public class JsonHelper
{
// 序列化
public static string JsonSerializer<T>(T t)
{
// 使用 DataContractJsonSerializer 将 T 对象序列化为内存流。
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof (T));
MemoryStream ms = new MemoryStream();
// 使用 WriteObject 方法将 JSON 数据写入到流中。
jsonSerializer.WriteObject(ms, t);
// 流转字符串
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
//替换Json的Date字符串
string p = @"\\/Date\((\d+)\+\d+\)\\/";
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
Regex reg = new Regex(p);
jsonString = reg.Replace(jsonString, matchEvaluator);
return jsonString;
}
public static T JsonDeserialize<T>(string jsonString)
{
//将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式
string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
Regex reg = new Regex(p);
jsonString = reg.Replace(jsonString, matchEvaluator);
DataContractJsonSerializer jsonSerializer=new DataContractJsonSerializer(typeof(T));
// 字符串转流
MemoryStream ms=new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
// 通过使用 DataContractJsonSerializer 的 ReadObject 方法,将 JSON 编码数据反序列化为T
T obj = (T) jsonSerializer.ReadObject(ms);
return obj;
}
public static string ConvertJsonDateToDateString(Match match)
{
string result = string.Empty;
DateTime dateTime=new DateTime(1970,1,1);
dateTime = dateTime.AddMilliseconds(long.Parse(match.Groups[1].Value));
dateTime = dateTime.ToLocalTime();
result = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
return result;
}
private static string ConvertDateStringToJsonDate(Match m)
{
string result = string.Empty;
DateTime dt = DateTime.Parse(m.Groups[0].Value);
dt = dt.ToUniversalTime();
TimeSpan ts = dt - DateTime.Parse("1970-01-01");
result = string.Format("\\/Date({0}+0800)\\/",ts.TotalMilliseconds);
return result;
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime dateTime { get; set; }
}
3. 后台代码实现
- protected void Page_Load(object sender, EventArgs e)
- {
- // 序列化
- Person p = new Person();
- p.Name = "Bradley";
- p.Age = 24;
- p.dateTime = DateTime.Now;
- string jsonString = JsonHelper.JsonSerializer<Person>(p);
- Response.Write(jsonString);
- Response.Write("</br>");
- // 反序列化
- string jsonString1 = "{\"Age\":28,\"Name\":\"张三\"}";
- Person p1 = JsonHelper.JsonDeserialize<Person>(jsonString);
- Response.Write(p1.Name+":"+p1.Age.ToString()+"Time:"+p.dateTime.ToString());
- // 序列化字典
- Dictionary<string,string> dictionary=new Dictionary<string, string>();
- dictionary.Add("Name:","Bradley");
- dictionary.Add("Age:","24");
- string jsonString2 = JsonHelper.JsonSerializer<Dictionary<string, string>>(dictionary);
- Response.Write("</br>");
- Response.Write(jsonString2);
- }
protected void Page_Load(object sender, EventArgs e)
{
// 序列化
Person p = new Person();
p.Name = "Bradley";
p.Age = 24;
p.dateTime = DateTime.Now;
string jsonString = JsonHelper.JsonSerializer<Person>(p);
Response.Write(jsonString);
Response.Write("</br>");
// 反序列化
string jsonString1 = "{\"Age\":28,\"Name\":\"张三\"}";
Person p1 = JsonHelper.JsonDeserialize<Person>(jsonString);
Response.Write(p1.Name+":"+p1.Age.ToString()+"Time:"+p.dateTime.ToString());
// 序列化字典
Dictionary<string,string> dictionary=new Dictionary<string, string>();
dictionary.Add("Name:","Bradley");
dictionary.Add("Age:","24");
string jsonString2 = JsonHelper.JsonSerializer<Dictionary<string, string>>(dictionary);
Response.Write("</br>");
Response.Write(jsonString2);
}