Beispiel #1
0
        public static string DataTable2Json(DataTable dt)
        {
            string result;

            if (dt != null && dt.Rows.Count > 0)
            {
                result = JSONConverter.DataTable2Json(dt.Rows.Count, true, "", "", dt);
            }
            else
            {
                result = "{success:false}";
            }
            return(result);
        }
Beispiel #2
0
 private static void WriteDataRow(System.Text.StringBuilder sb, DataRow row)
 {
     sb.Append("{");
     foreach (DataColumn dataColumn in row.Table.Columns)
     {
         sb.AppendFormat("\"{0}\":", dataColumn.ColumnName);
         JSONConverter.WriteValue(sb, row[dataColumn]);
         sb.Append(",");
     }
     if (row.Table.Columns.Count > 0)
     {
         sb.Length--;
     }
     sb.Append("}");
 }
Beispiel #3
0
        public static string DataTable2JsonForm(DataTable dt)
        {
            string result;

            if (dt != null && dt.Rows.Count > 0)
            {
                System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                JSONConverter.WriteFormDataTable(stringBuilder, dt);
                result = stringBuilder.ToString();
            }
            else
            {
                result = "{success:false}";
            }
            return(result);
        }
Beispiel #4
0
 public static void WriteValue(System.Text.StringBuilder sb, object val)
 {
     if (val == null || val == System.DBNull.Value)
     {
         sb.Append("\"null\"");
     }
     else if (val is string || val is System.Guid)
     {
         JSONConverter.WriteString(sb, val.ToString());
     }
     else if (val is bool)
     {
         JSONConverter.WriteString(sb, val.ToString());
     }
     else if (val is double || val is float || val is long || val is int || val is short || val is byte || val is decimal)
     {
         JSONConverter.WriteString(sb, val.ToString());
     }
     else if (val.GetType().IsEnum)
     {
         sb.Append("\"" + (int)val + "\"");
     }
     else if (val is DateTime)
     {
         JSONConverter.WriteString(sb, ((DateTime)val).ToString("yyyy-MM-dd HH:mm:ss", new System.Globalization.CultureInfo("en-US", false).DateTimeFormat));
     }
     else if (val is DataTable)
     {
         JSONConverter.WriteDataTable(sb, val as DataTable);
     }
     else if (val is DataRow)
     {
         JSONConverter.WriteDataRow(sb, val as DataRow);
     }
     else if (val is System.Collections.Hashtable)
     {
         JSONConverter.WriteHashtable(sb, val as System.Collections.Hashtable);
     }
     else if (val is System.Collections.IEnumerable)
     {
         JSONConverter.WriteEnumerable(sb, val as System.Collections.IEnumerable);
     }
     else
     {
         JSONConverter.WriteObject(sb, val);
     }
 }
Beispiel #5
0
        private static void WriteEnumerable(System.Text.StringBuilder sb, System.Collections.IEnumerable e)
        {
            bool flag = false;

            sb.Append("[");
            foreach (object current in e)
            {
                JSONConverter.WriteValue(sb, current);
                sb.Append(",");
                flag = true;
            }
            if (flag)
            {
                sb.Length--;
            }
            sb.Append("]");
        }
Beispiel #6
0
 private static void WriteFormDataTable(System.Text.StringBuilder sb, DataTable table)
 {
     if (string.IsNullOrEmpty(sb.ToString()))
     {
         sb.Append("{\"success\":\"true\",\"dataList\":");
     }
     foreach (DataRow row in table.Rows)
     {
         JSONConverter.WriteDataRow(sb, row);
         sb.Append(",");
     }
     if (table.Rows.Count > 0)
     {
         sb.Length--;
     }
     sb.Append("}");
 }
Beispiel #7
0
 private static void WriteDataTable(System.Text.StringBuilder sb, DataTable table)
 {
     if (string.IsNullOrEmpty(sb.ToString()))
     {
         sb.Append("{data:[");
     }
     foreach (DataRow row in table.Rows)
     {
         JSONConverter.WriteDataRow(sb, row);
         sb.Append(",");
     }
     if (table.Rows.Count > 0)
     {
         sb.Length--;
     }
     sb.Append("]}");
 }
Beispiel #8
0
        private static void WriteHashtable(System.Text.StringBuilder sb, System.Collections.Hashtable e)
        {
            bool flag = false;

            sb.Append("{");
            foreach (string text in e.Keys)
            {
                sb.AppendFormat("\"{0}\":", text.ToLower());
                JSONConverter.WriteValue(sb, e[text]);
                sb.Append(",");
                flag = true;
            }
            if (flag)
            {
                sb.Length--;
            }
            sb.Append("}");
        }
Beispiel #9
0
        private static void WriteObject(System.Text.StringBuilder sb, object o)
        {
            System.Reflection.MemberInfo[] members = o.GetType().GetMembers(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
            sb.Append("{");
            bool flag = false;

            System.Reflection.MemberInfo[] array = members;
            for (int i = 0; i < array.Length; i++)
            {
                System.Reflection.MemberInfo memberInfo = array[i];
                bool   flag2 = false;
                object val   = null;
                if ((memberInfo.MemberType & System.Reflection.MemberTypes.Field) == System.Reflection.MemberTypes.Field)
                {
                    System.Reflection.FieldInfo fieldInfo = (System.Reflection.FieldInfo)memberInfo;
                    val   = fieldInfo.GetValue(o);
                    flag2 = true;
                }
                else if ((memberInfo.MemberType & System.Reflection.MemberTypes.Property) == System.Reflection.MemberTypes.Property)
                {
                    System.Reflection.PropertyInfo propertyInfo = (System.Reflection.PropertyInfo)memberInfo;
                    if (propertyInfo.CanRead && propertyInfo.GetIndexParameters().Length == 0)
                    {
                        val   = propertyInfo.GetValue(o, null);
                        flag2 = true;
                    }
                }
                if (flag2)
                {
                    sb.Append("\"");
                    sb.Append(memberInfo.Name);
                    sb.Append("\":");
                    JSONConverter.WriteValue(sb, val);
                    sb.Append(",");
                    flag = true;
                }
            }
            if (flag)
            {
                sb.Length--;
            }
            sb.Append("}");
        }
Beispiel #10
0
 public static string DataTable2Json(int totalCount, bool flag, string errmsg, string singleinfo, DataTable dt)
 {
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     if (flag && totalCount > 0 && dt != null && dt.Rows.Count > 0)
     {
         stringBuilder.Append(string.Concat(new object[]
         {
             "{\"totalCount\":\"",
             totalCount,
             "\",\"success\":\"",
             flag.ToString().ToLower(),
             "\",\"error\":\"",
             errmsg,
             "\",\"singleInfo\":\"",
             singleinfo,
             "\",\"dataList\":["
         }));
         JSONConverter.WriteValue(stringBuilder, dt);
     }
     else if (string.IsNullOrEmpty(errmsg))
     {
         stringBuilder.Append("{\"success\":\"" + flag.ToString().ToLower() + "\"}");
     }
     else
     {
         stringBuilder.Append(string.Concat(new string[]
         {
             "{\"success\":\"",
             flag.ToString().ToLower(),
             ",\"error\":\"",
             errmsg,
             "\"}"
         }));
     }
     return(stringBuilder.ToString());
 }
Beispiel #11
0
 public static string DataTable2Json(int totalCount, DataTable dt)
 {
     return(JSONConverter.DataTable2Json(totalCount, true, "", "", dt));
 }
Beispiel #12
0
 public static string Convert2Json(object o)
 {
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     JSONConverter.WriteValue(stringBuilder, o);
     return(stringBuilder.ToString());
 }