C#实现简单的JSON序列化功能代码实例
好久没有做web了,JSON目前比较流行,闲得没事,所以动手试试将对象序列化为JSON字符(尽管DotNet Framework已经有现成的库,也有比较好的第三方开源库),而且只是实现了处理简单的类型,并且DateTime处理的也不专业,有兴趣的筒子可以扩展,代码比较简单,反序列化木有实现:( ,直接贴代码吧,都有注释了,所以废话不多说 :) 复制代码 代码如下: 测试类/// <summary> /// Nested class of Person. /// </summary> public class House { public string Name { get; set; } public double Price { get; set; } } /// <summary> public bool IsMarried public string[] Names public int[] Ages 复制代码 代码如下: 接口定义 /// <summary> /// IJsonSerializer interface. /// </summary> interface IJsonSerializer { /// <summary> /// Serialize object to json string. /// </summary> /// <typeparam name="T">The type to be serialized.</typeparam> /// <param name="obj">Instance of the type T.</param> /// <returns>json string.</returns> string Serialize(object obj); /// <summary> 接口实现,还有待完善.. 复制代码 代码如下: /// <summary> /// Implement IJsonSerializer,but Deserialize had not been implemented. /// </summary> public class JsonSerializer : IJsonSerializer { /// <summary> /// Serialize object to json string. /// </summary> /// <typeparam name="T">The type to be serialized.</typeparam> /// <param name="obj">Instance of the type T.</param> /// <returns>json string.</returns> public string Serialize(object obj) { if (obj == null) { return "{}"; } // Get the type of obj. // Just deal with the public instance properties. others ignored. PropertyInfo[] pis = t.GetProperties(bf); StringBuilder json = new StringBuilder("{"); if (pis != null && pis.Length > 0) foreach (PropertyInfo p in pis) if (o == null) json.AppendFormat(""{0}":{1}",subJsString); json.AppendFormat(""{0}":{1}",subJsString); if (dt == default(DateTime)) if (i >= 0 && i != lastIndex) ++i; json.Append("}"); return json.ToString(); /// <summary> /// <summary> object firstElement = obj.GetValue(0); StringBuilder sb = new StringBuilder("["); if (quotable) if (index >= 0 && index != lastIndex) ++index; if (index >= 0 && index != lastIndex) ++index; sb.Append("]"); return sb.ToString(); return "null"; /// <summary> object firstElement = obj[0]; StringBuilder sb = new StringBuilder("["); if (quotable) ++index; sb.Append("]"); return sb.ToString(); return "null"; /// <summary> return false; /// <summary> return false; 测试代码: 复制代码 代码如下: class Program { static void Main(string[] args) { Person ps = new Person() { Name = "Leon", Age = 25, Address = "China", IsMarried = false, Names = new string[] { "wgc","leon","giantfish" }, Ages = new int[] { 1,2,3,4 }, MyHouse = new House() { Name = "HouseName", Price = 100.01, }, BirthDay = new DateTime(1986,12,20,10), Friends = new List<string>() { "friend1","friend2" }, LoveNumbers = new List<int>() { 1,3 } }; IJsonSerializer js = new JsonSerializer(); 生成的 JSON字符串 :
复制代码 代码如下: {"Name":"Leon","Age":25,"Address":"China","IsMarried":false,"Names":["wgc","giantfish"],"Ages":[1,4],"MyHouse":{"Name":"HouseName","Price":100.01},"BirthDay":"1986-12-20 12:12:10","Friends":["friend1","friend2"],"LoveNumbers":[1,3]} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |