json转换

        #region ParseToJson

        public static string ParseToJson(DataTable dt)
        {
            StringBuilder JsonString = new StringBuilder();
            //Exception Handling        
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + FilterJson(dt.Rows[i][j].ToString()) + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + FilterJson(dt.Rows[i][j].ToString()) + "\"");
                        }
                    }
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]");
                return JsonString.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        public static string ParseToJson<T>(List<T> list, int rowCount)
        {
            string jsonStr = string.Empty;
            if (rowCount > 0)
            {
                jsonStr = ParseToJson<T>(list);
                jsonStr = "{ \"rowCount\": \"" + rowCount + "\", \"dataList\": " + jsonStr + " }";
            }
            else
            {
                jsonStr = "{ \"rowCount\": \"0\"}";
            }
            return jsonStr;
        }
        public static string ParseToJson<T>(List<T> list)
        {
            if (list != null && list.Count > 0)
            {
                PropertyInfo[] propertys = list[0].GetType().GetProperties();
                // json
                StringBuilder jsonStr = new StringBuilder();
                jsonStr.Append("[");
                for (int m = 0; m < list.Count; m++)
                {
                    jsonStr.Append("{");
                    for (int i = 0; i < propertys.Length; i++)
                    {
                        var vl = propertys[i].GetValue(list[m], null) ;
                        vl = vl == DBNull.Value ? "" : propertys[i].GetValue(list[m], null);
                        vl = vl ?? "";
                        jsonStr.AppendFormat("\"{0}\":\"{1}\"", propertys[i].Name, Utils2.FilterJson(vl.ToString()));
                        if (i != propertys.Length - 1)
                        {
                            jsonStr.Append(",");
                        }
                    }
                    jsonStr.Append("}");
                    if (m != list.Count - 1)
                    {
                        jsonStr.Append(",");
                    }
                }
                jsonStr.Append("]");
                return jsonStr.ToString();
            }
            return string.Empty;
        }
        public static string ParseToJson(DataTable dt, int rowCount)
        {
            string jsonStr = string.Empty;
            if (rowCount > 0)
            {
                jsonStr = ParseToJson(dt);
                jsonStr = "{ \"rowCount\": \"" + rowCount + "\", \"dataList\": " + jsonStr + " }";
            }
            else
            {
                jsonStr = "{ \"rowCount\": \"0\"}";
            }
            return jsonStr;
        }
        #endregion

        #region ParseToJson { rowCount:0 }
        public static string ParseToJson(int rowCount, DataTable dt)
        {
            string jsonStr = string.Empty;
            if (dt != null && dt.Rows.Count > 0)
            {
                jsonStr = ParseToJson(dt);
                jsonStr = "{ \"rowCount\": \"" + rowCount + "\", \"dataList\": " + jsonStr + " }";
            }
            else
            {
                jsonStr = "{ \"rowCount\": \"0\"}";
            }
            return jsonStr;
        }

        public static string ParseToJson(object value)
        {
            string jsonStr = value != null ? value.ToString() : string.Empty;
            return "{\"jsonMsg\":\"" + jsonStr + "\"}";
        }
        //权限
        public static string PerssionTojson(object value)
        {
            string jsonStr = value != null ? value.ToString() : string.Empty;
            return "{\"Perssion\":\"" + jsonStr + "\"}";
        }
        //权限
        public static string SessionTojson(object value)
        {
            string jsonStr = value != null ? value.ToString() : string.Empty;
            return "{\"session\":\"" + jsonStr + "\"}";
        }
        public static string LigeruiDataToJson<T>(List<T> list, int rowCount)
        {
            string jsonStr = string.Empty;
            if (rowCount > 0)
            {
                jsonStr = ParseToJson<T>(list);
                jsonStr = "{ \"Rows\":" + jsonStr + ",\"Total\":" + rowCount + " }";
            }
            else
            {
                jsonStr = "{ \"Rows\":0,\"Total\":0 }";
            }
            return jsonStr;
        }
        public static string LigeruiDataToJson(DataTable dt, int rowCount)
        {
            string jsonStr = string.Empty;
            if (rowCount > 0)
            {
                jsonStr = ParseToJson(dt);
                jsonStr = "{ \"Rows\":" + jsonStr + ",\"Total\":" + rowCount + " }";
            }
            else
            {
                jsonStr = "{ \"Rows\":0,\"Total\":0 }";
            }
            return jsonStr;
        }


        public static string FilterJson(string s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {

                char c = s[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':
                        sb.Append("\\\\");
                        break;
                    case '/':
                        sb.Append("\\/");
                        break;
                    case '\b':
                        sb.Append("\\b");
                        break;
                    case '\f':
                        sb.Append("\\f");
                        break;
                    case '\n':
                        //sb.Append("\\n");
                        sb.Append("<br />");
                        break;
                    case '\r':
                        sb.Append("\\r");
                        break;
                    case '\t':
                        sb.Append("\\t");
                        break;
                    default:
                        sb.Append(c);
                        break;
                }

            }
            return sb.ToString();
        }

        #endregion

 

posted on 2015-05-06 15:02  雪原日暮  阅读(164)  评论(0编辑  收藏  举报