/// <summary> /// 将属性写入Json串 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public JsonHelper AddObjectToJson(string key, string value) { #region string json = ""; //有bug,如果字符串内包含byte[],会出错哦,待修正?? //if (value.IndexOf("[") != -1 && value.IndexOf("]") != -1) // json = "{\"" + key + "\":" + JsonFilter.Exec(value) + "}"; //else json = "{\"" + key + "\":\"" + JsonFilter.Exec(value) + "\"}"; this.addModule(json); return(this); #endregion }
/// <summary> /// 重写序列化方式 /// </summary> /// <param name="obj"></param> /// <param name="serializer"></param> /// <returns></returns> private static string CreateSerialize(DataTable dt, string jsonTablename) { #region string result = ""; string rowjson = ""; string topicname = (jsonTablename == null) ? "topics" : jsonTablename; foreach (DataRow dr in dt.Rows) { string coljson = ""; string tmpValue = ""; foreach (DataColumn dc in dt.Columns) { if (dc.DataType == Type.GetType("System.Boolean")) { tmpValue = (dr[dc.ColumnName] == System.DBNull.Value) ? "null" : Convert.ToBoolean(dr[dc.ColumnName]).ToString().ToLower(); coljson += String.Format("\"{0}\":{1},", dc.ColumnName, tmpValue); } else if (dc.DataType == Type.GetType("System.DateTime")) { tmpValue = (dr[dc.ColumnName] == System.DBNull.Value) ? "" : Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd HH:mm:ss"); coljson += String.Format("\"{0}\":\"{1}\",", dc.ColumnName, tmpValue); } else { coljson += String.Format("\"{0}\":\"{1}\",", dc.ColumnName, JsonFilter.Exec(dr[dc.ColumnName].ToString())); } } coljson = coljson.Remove(coljson.Length - 1, 1); rowjson += "{" + coljson + "},"; } if (dt.Rows.Count > 0) { rowjson = rowjson.Remove(rowjson.Length - 1, 1); } result = "{\"" + topicname + "\":[" + rowjson + "]}"; return(result); #endregion }
/// <summary> /// 返回错误 /// </summary> /// <returns></returns> public void WriteError(Exception e) { this._jsonString = ""; this.AddObjectToJson("success", "false"); this.AddObjectToJson("msg", JsonFilter.Exec(e.Message)); }