json学习笔记(4)
修改jsonhelper类需用到的名称空间:
View Code
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;//List名称空间
jsonhelper类的修改:
View Code
public class JsonHelper
{
/// <summary>
/// json序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string JsonSerializer<T>(T t)
{
DataContractJsonSerializer zer = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
zer.WriteObject(ms, t);
string jsonstring = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
string p = @"\\/Date\((\d+)\+\d+\)\\/";
MatchEvaluator me = new MatchEvaluator(ConvertJsonDateToDateString);
Regex rg = new Regex(p);
jsonstring = rg.Replace(jsonstring, me);
return jsonstring;
}
/// <summary>
/// json反序列化
/// </summary>
/// <typeparam name="T" _cke_saved_name="T"></typeparam>
/// <param name="jsonstring"></param>
/// <returns></returns>
public static T JsonDeserializer<T>(string jsonstring)
{
string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
MatchEvaluator me = new MatchEvaluator(ConvertDateTimeToJsonDate);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>