JsonHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Converters;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Text;
  7. using System.Web.Script.Serialization;
  8. namespace HNWD.Pregrant.Common
  9. {
  10. public static class JsonHelper
  11. {
  12. public static string Serialize(Object o, string str2)
  13. {
  14. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  15. Serializer.MaxJsonLength = Int32.MaxValue;
  16. string str = Serializer.Serialize(o);
  17. return str;
  18. }
  19. public static string Serialize(Object o)
  20. {
  21. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  22. Serializer.MaxJsonLength = Int32.MaxValue;
  23. string str = Serializer.Serialize(o);
  24. return str;
  25. }
  26. public static string SerializeWithSuffix(Object o, string Suffix = ",OK!")
  27. {
  28. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  29. Serializer.MaxJsonLength = int.MaxValue;
  30. string str = Serializer.Serialize(o);
  31. return str + Suffix;
  32. }
  33. public static string Serialize(DataSet o)
  34. {
  35. string str = JsonConvert.SerializeObject(o, new DataTableConverter());
  36. str = str.Replace("Table", "dataList");
  37. return str;
  38. }
  39. public static string Serialize(DataTable table, string type = "javascript")
  40. {
  41. JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
  42. List<Dictionary<string, object>> parentRow = new List<Dictionary<string, object>>();
  43. Dictionary<string, object> childRow;
  44. foreach (DataRow row in table.Rows)
  45. {
  46. childRow = new Dictionary<string, object>();
  47. foreach (DataColumn col in table.Columns)
  48. {
  49. childRow.Add(col.ColumnName, row[col]);
  50. }
  51. parentRow.Add(childRow);
  52. }
  53. return jsSerializer.Serialize(parentRow);
  54. }
  55. public static string Serialize(DataTable table, string type = "net", string type2 = "net")
  56. {
  57. string jsonString = JsonConvert.SerializeObject(table);
  58. return jsonString;
  59. }
  60. public static string Serialize(DataTable table, string type = "string", string type2 = "string", string type3 = "string")
  61. {
  62. var jsonString = new StringBuilder();
  63. jsonString.Append("{\"" + table.TableName + "\":");
  64. if (table.Rows.Count > 0)
  65. {
  66. jsonString.Append("[");
  67. for (int i = 0; i < table.Rows.Count; i++)
  68. {
  69. jsonString.Append("{");
  70. for (int j = 0; j < table.Columns.Count; j++)
  71. {
  72. if (j < table.Columns.Count - 1)
  73. {
  74. jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
  75. + "\":" + "\""
  76. + table.Rows[i][j].ToString() + "\",");
  77. }
  78. else if (j == table.Columns.Count - 1)
  79. {
  80. jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
  81. + "\":" + "\""
  82. + table.Rows[i][j].ToString() + "\"");
  83. }
  84. }
  85. if (i == table.Rows.Count - 1)
  86. {
  87. jsonString.Append("}");
  88. }
  89. else
  90. {
  91. jsonString.Append("},");
  92. }
  93. }
  94. jsonString.Append("]");
  95. jsonString.Append("}");
  96. }
  97. return jsonString.ToString();
  98. }
  99. public static T Deserialize<T>(string json)
  100. {
  101. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  102. T objs = Serializer.Deserialize<T>(json);
  103. return objs;
  104. }
  105. public static T DeserializeWithSuffix<T>(string json, string Suffix = ",OK!")
  106. {
  107. json = json.Replace(Suffix, "");
  108. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  109. T objs = Serializer.Deserialize<T>(json);
  110. return objs;
  111. }
  112. }
  113. }