经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
JSONHelper - hi-latolo
来源:cnblogs  作者:hi-latolo  时间:2018/12/7 9:38:02  对本文有异议

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;



  1. [Serializable]
  2. public static class JSONHelper
  3. {
  4. /// <summary>
  5. /// 对象转JSON
  6. /// </summary>
  7. /// <param name="obj">对象</param>
  8. /// <returns>JSON格式的字符串</returns>
  9. public static string ObjectToJSON(object obj)
  10. {
  11. JavaScriptSerializer jss = new JavaScriptSerializer();
  12. try
  13. {
  14. return jss.Serialize(obj);
  15. }
  16. catch { }
  17. return null;
  18. }
  19.  
  20. /// <summary>
  21. ///
  22. /// JSON文本转对象,泛型方法
  23. /// </summary>
  24. /// <typeparam name="T">类型</typeparam>
  25. /// <param name="jsonText">JSON文本</param>
  26. /// <returns>指定类型的对象</returns>
  27. public static T JSONToObject<T>(string jsonText)
  28. {
  29. JavaScriptSerializer jss = new JavaScriptSerializer();
  30. try
  31. {
  32. return jss.Deserialize<T>(jsonText);
  33. }
  34. catch{}
  35. return default(T);
  36. }
  37.  
  38. public static string ObjectToJsonDateTime(object obj)
  39. {
  40. JavaScriptSerializer jss = new JavaScriptSerializer();
  41. jss.RegisterConverters(new JavaScriptConverter[] { new DateTimeConverter() });
  42. try
  43. {
  44. return jss.Serialize(obj);
  45. }
  46. catch { }
  47. return null;
  48. }
  49. }


  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web.Script.Serialization;
  7.  
  8.  
  9. public class DateTimeConverter : JavaScriptConverter
  10. {
  11. public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
  12. {
  13. return new JavaScriptSerializer().ConvertToType(dictionary, type);
  14. }
  15.  
  16. public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
  17. {
  18. if (!(obj is DateTime))
  19. {
  20. return null;
  21. }
  22. return new CustomString(((DateTime)obj).ToString("yyyy-MM-dd HH:mm:ss"));
  23. }
  24.  
  25. public override IEnumerable<Type> SupportedTypes
  26. {
  27. get
  28. {
  29. return new[] { typeof(DateTime) };
  30. }
  31. }
  32.  
  33. private class CustomString : Uri, IDictionary<string, object>
  34. {
  35. public CustomString(string str) : base(str, UriKind.Relative)
  36. {
  37. }
  38.  
  39. void IDictionary<string, object>.Add(string key, object value)
  40. {
  41. throw new NotImplementedException();
  42. }
  43.  
  44. bool IDictionary<string, object>.ContainsKey(string key)
  45. {
  46. throw new NotImplementedException();
  47. }
  48.  
  49. ICollection<string> IDictionary<string, object>.Keys
  50. {
  51. get
  52. {
  53. throw new NotImplementedException();
  54. }
  55.  
  56. }
  57.  
  58. bool IDictionary<string, object>.Remove(string key)
  59. {
  60. throw new NotImplementedException();
  61. }
  62.  
  63.  
  64. bool IDictionary<string, object>.TryGetValue(string key, out object value)
  65. {
  66. throw new NotImplementedException();
  67.  
  68. }
  69.  
  70. ICollection<object> IDictionary<string, object>.Values
  71. {
  72.  
  73. get
  74. {
  75. throw new NotImplementedException();
  76. }
  77.  
  78. }
  79.  
  80. object IDictionary<string, object>.this[string key]
  81. {
  82. get
  83. {
  84. throw new NotImplementedException();
  85. }
  86. set
  87. {
  88. throw new NotImplementedException();
  89. }
  90. }
  91.  
  92. void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
  93. {
  94. throw new NotImplementedException();
  95. }
  96.  
  97. void ICollection<KeyValuePair<string, object>>.Clear()
  98. {
  99. throw new NotImplementedException();
  100. }
  101.  
  102. bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
  103. {
  104.  
  105. throw new NotImplementedException();
  106.  
  107. }
  108.  
  109. void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
  110. {
  111. throw new NotImplementedException();
  112.  
  113. }
  114.  
  115. int ICollection<KeyValuePair<string, object>>.Count
  116. {
  117. get
  118. {
  119. throw new NotImplementedException();
  120. }
  121. }
  122.  
  123. bool ICollection<KeyValuePair<string, object>>.IsReadOnly
  124. {
  125. get
  126. {
  127. throw new NotImplementedException();
  128. }
  129. }
  130.  
  131. bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
  132. {
  133. throw new NotImplementedException();
  134. }
  135.  
  136. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
  137. {
  138. throw new NotImplementedException();
  139. }
  140.  
  141. IEnumerator IEnumerable.GetEnumerator()
  142. {
  143. throw new NotImplementedException();
  144. }
  145. }
  146. }

  

  

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号