zoukankan      html  css  js  c++  java
  • 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 '':
                            sb.Append("\b");
                            break;
                        case 'f':
                            sb.Append("\f");
                            break;
                        case '
    ':
                            //sb.Append("\n");
                            sb.Append("<br />");
                            break;
                        case '
    ':
                            sb.Append("\r");
                            break;
                        case '	':
                            sb.Append("\t");
                            break;
                        default:
                            sb.Append(c);
                            break;
                    }
    
                }
                return sb.ToString();
            }
    
            #endregion
  • 相关阅读:
    怎么样从多列的DataTable里取需要的几列
    .net core 生成二维码
    sql server2012卸载
    github实用的搜索小技巧
    c# 中的索引
    IOC
    Python基础-while
    Python基础-判断闰年
    Python基础-while奇数和
    Python基础-奇偶判断调用函数
  • 原文地址:https://www.cnblogs.com/muxueyuan/p/4482062.html
Copyright © 2011-2022 走看看