1 /// <summary> 2 /// Json帮助类 3 /// </summary> 4 public class JsonHelper 5 { 6 /// <summary> 7 /// 将对象序列化为JSON格式 8 /// </summary> 9 /// <param name="o">对象</param> 10 /// <returns>json字符串</returns> 11 public static string SerializeObject(object o) 12 { 13 string json = JsonConvert.SerializeObject(o); 14 return json; 15 } 16 /// <summary> 17 /// json 转换为object 18 /// </summary> 19 /// <param name="json"></param> 20 /// <returns></returns> 21 public static object DeserializeObject(string json) 22 { 23 return JsonConvert.DeserializeObject(json); 24 } 25 /// <summary> 26 /// 将对象序列化为JSON格式 27 /// </summary> 28 /// <param name="o">对象</param> 29 /// <returns>json字符串</returns> 30 public static string SerializeDatatable(DataTable dt) 31 { 32 //string json = JsonConvert.SerializeObject(dt, new DataTableConverter()); 33 //return json; 34 StringBuilder JsonString = new StringBuilder(); 35 //Exception Handling 36 if (dt != null && dt.Rows.Count > 0) 37 { 38 JsonString.Append("[ "); 39 for (int i = 0; i < dt.Rows.Count; i++) 40 { 41 JsonString.Append("{ "); 42 for (int j = 0; j < dt.Columns.Count; j++) 43 { 44 if (j < dt.Columns.Count - 1) 45 { 46 JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\","); 47 } 48 else if (j == dt.Columns.Count - 1) 49 { 50 JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\""); 51 } 52 } 53 /*end Of String*/ 54 if (i == dt.Rows.Count - 1) 55 { 56 JsonString.Append("} "); 57 } 58 else 59 { 60 JsonString.Append("}, "); 61 } 62 } 63 JsonString.Append("]"); 64 return JsonString.ToString(); 65 } 66 else 67 { 68 return null; 69 } 70 } 71 72 73 74 /// <summary> 75 /// 解析JSON字符串生成对象实体 76 /// </summary> 77 /// <typeparam name="T">对象类型</typeparam> 78 /// <param name="json">json字符串(eg.{"ID":"112","Name":"石子儿"})</param> 79 /// <returns>对象实体</returns> 80 public static T DeserializeJsonToObject<T>(string json) where T : class 81 { 82 JsonSerializer serializer = new JsonSerializer(); 83 StringReader sr = new StringReader(json); 84 object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T)); 85 T t = o as T; 86 return t; 87 } 88 89 /// <summary> 90 /// 解析JSON数组生成对象实体集合 91 /// </summary> 92 /// <typeparam name="T">对象类型</typeparam> 93 /// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param> 94 /// <returns>对象实体集合</returns> 95 public static List<T> DeserializeJsonToList<T>(string json) where T : class 96 { 97 JsonSerializer serializer = new JsonSerializer(); 98 StringReader sr = new StringReader(json); 99 object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>)); 100 List<T> list = o as List<T>; 101 return list; 102 } 103 104 /// <summary> 105 /// 反序列化JSON到给定的匿名对象. 106 /// </summary> 107 /// <typeparam name="T">匿名对象类型</typeparam> 108 /// <param name="json">json字符串</param> 109 /// <param name="anonymousTypeObject">匿名对象</param> 110 /// <returns>匿名对象</returns> 111 public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject) 112 { 113 T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject); 114 return t; 115 } 116 117 #region dataTable转换成Json格式 118 /// <summary> 119 /// dataTable转换成Json格式 120 /// </summary> 121 /// <param name="dt"></param> 122 /// <returns></returns> 123 public static string DataTableToJson(DataTable dt) 124 { 125 StringBuilder jsonBuilder = new StringBuilder(); 126 jsonBuilder.Append("{\""); 127 jsonBuilder.Append(dt.TableName.ToString()); 128 jsonBuilder.Append("\":["); 129 for (int i = 0; i < dt.Rows.Count; i++) 130 { 131 jsonBuilder.Append("{"); 132 for (int j = 0; j < dt.Columns.Count; j++) 133 { 134 jsonBuilder.Append("\""); 135 jsonBuilder.Append(dt.Columns[j].ColumnName); 136 jsonBuilder.Append("\":\""); 137 jsonBuilder.Append(dt.Rows[i][j].ToString()); 138 jsonBuilder.Append("\","); 139 } 140 jsonBuilder.Remove(jsonBuilder.Length - 1, 1); 141 jsonBuilder.Append("},"); 142 } 143 jsonBuilder.Remove(jsonBuilder.Length - 1, 1); 144 jsonBuilder.Append("]"); 145 jsonBuilder.Append("}"); 146 return jsonBuilder.ToString(); 147 } 148 149 #endregion dataTable转换成Json格式 150 151 #region DataSet转换成Json格式 152 /// <summary> 153 /// DataSet转换成Json格式 154 /// </summary> 155 /// <param name="ds">DataSet</param> 156 /// <returns></returns> 157 public static string DataSetToJson(DataSet ds) 158 { 159 StringBuilder json = new StringBuilder(); 160 161 foreach (DataTable dt in ds.Tables) 162 { 163 json.Append("{\""); 164 json.Append(dt.TableName); 165 json.Append("\":"); 166 json.Append(DataTableToJson(dt)); 167 json.Append("}"); 168 } 169 return json.ToString(); 170 } 171 #endregion 172 }