关键代码:
using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Web.Script.Serialization; namespace YanZhiwei.DotNet3._5.Utilities.Common { /// <summary> /// JavaScriptSerializer 帮助类 /// </summary> public static class ScriptSerializerHelper { #region 处理Json字符串的时间格式问题 /// <summary> /// 处理JsonString的时间格式问题 /// <para>eg:ScriptSerializerHelper.ParseJsonTime(@"[{‘getTime‘:‘\/Date(1419564257428)\/‘}]", "yyyyMMdd hh:mm:ss");==>[{‘getTime‘:‘20141226 11:24:17‘}]</para> /// <para>参考:http://www.cnphp6.com/archives/35773 </para> /// </summary> /// <param name="jsonString">Json字符串</param> /// <param name="formart">时间格式化类型</param> /// <returns>处理好的Json字符串</returns> public static string ParseJsonTime(this string jsonString, string formart) { if (!string.IsNullOrEmpty(jsonString)) { jsonString = Regex.Replace(jsonString, @"\\/Date\((\d+)\)\\/", match => { DateTime _dateTime = new DateTime(1970, 1, 1); _dateTime = _dateTime.AddMilliseconds(long.Parse(match.Groups[1].Value)); _dateTime = _dateTime.ToLocalTime(); return _dateTime.ToString(formart); }); } return jsonString; } /// <summary> /// 处理JsonString的时间格式问题【时间格式:yyyy-MM-dd HH:mm:ss】 /// <para>参考:http://www.cnphp6.com/archives/35773 </para> /// </summary> /// <param name="jsonString">Json字符串</param> /// <returns>处理好的Json字符串</returns> public static string ParseJsonTime(this string jsonString) { return ParseJsonTime(jsonString, "yyyy-MM-dd HH:mm:ss"); } #endregion #region 利用JavaScriptSerializer将对象序列化成JSON /// <summary> /// 利用JavaScriptSerializer将对象序列化成JSON字符串 /// <para>eg:ScriptSerializerHelper.Serialize<Person>(_personList);</para> /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="entityList">对象集合</param> /// <returns>json</returns> public static string Serialize<T>(this IEnumerable<T> entityList) where T : class { string _jsonString = string.Empty; if (entityList != null) { JavaScriptSerializer _serializerHelper = new JavaScriptSerializer(); _serializerHelper.MaxJsonLength = int.MaxValue; _jsonString = _serializerHelper.Serialize(entityList); } return _jsonString; } #endregion #region 利用JavaScriptSerializer将json字符串反序列化 /// <summary> ///利用JavaScriptSerializer将json字符串反序列化 /// <para>eg:List<Person> _result = (List<Person>)ScriptSerializerHelper.Deserialize<Person>(_jsonString);</para> /// </summary> /// <typeparam name="T">泛型</typeparam> /// <param name="jsonString"></param> /// <returns>IEnumerable</returns> public static IEnumerable<T> Deserialize<T>(this string jsonString) where T : class { IEnumerable<T> _list = null; if (!string.IsNullOrEmpty(jsonString)) { JavaScriptSerializer _serializerHelper = new JavaScriptSerializer(); _list = _serializerHelper.Deserialize<IEnumerable<T>>(jsonString); } return _list; } #endregion } }
代码测试:
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections; using System.Collections.Generic; using System.Linq; using YanZhiwei.DotNet3._5.UtilitiesTests.Model; namespace YanZhiwei.DotNet3._5.Utilities.Common.Tests { [TestClass()] public class ScriptSerializerHelperTests { [TestMethod()] public void SerializeTest() { Person _personA = new Person() { Name = "YanZhiweiA", Age = 10, Address = "shanghaiA" }; Person _personB = new Person() { Name = "YanZhiweiB", Age = 11, Address = "shanghaiB" }; IList<Person> _personList = new List<Person>(); _personList.Add(_personA); _personList.Add(_personB); string _actual = ScriptSerializerHelper.Serialize<Person>(_personList); string _expect = "[{\"Name\":\"YanZhiweiA\",\"Age\":10,\"Address\":\"shanghaiA\"},{\"Name\":\"YanZhiweiB\",\"Age\":11,\"Address\":\"shanghaiB\"}]"; Assert.AreEqual<string>(_expect, _actual); } [TestMethod()] public void DeserializeTest() { Person _personA = new Person() { Name = "YanZhiweiA", Age = 10, Address = "shanghaiA" }; Person _personB = new Person() { Name = "YanZhiweiB", Age = 11, Address = "shanghaiB" }; List<Person> _expected = new List<Person>(); _expected.Add(_personA); _expected.Add(_personB); string _jsonString = "[{‘Name‘:‘YanZhiweiA‘,‘Age‘:10,‘Address‘:‘shanghaiA‘},{‘Name‘:‘YanZhiweiB‘,‘Age‘:11,‘Address‘:‘shanghaiB‘}]"; List<Person> _result = (List<Person>)ScriptSerializerHelper.Deserialize<Person>(_jsonString); bool _actual = _expected.SequenceEqual(_result, new PersonCompare()); Assert.IsTrue(_actual); } [TestMethod()] public void ParseJsonTimeTest() { string _actual = ScriptSerializerHelper.ParseJsonTime(@"[{‘getTime‘:‘\/Date(1419564257428)\/‘}]", "yyyyMMdd hh:mm:ss"); Assert.AreEqual("[{‘getTime‘:‘20141226 11:24:17‘}]", _actual); } } public class PersonCompare : IEqualityComparer<Person> { public bool Equals(Person x, Person y) { return (x.Age == y.Age) && (x.Address == y.Address) && (x.Name == y.Name); } public int GetHashCode(Person obj) { return obj.Name.GetHashCode(); } } }
测试结果:
希望有所帮助!谢谢
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
时间: 2024-10-29 11:12:06