zoukankan      html  css  js  c++  java
  • .net的dataset,datatable,object等对象转json方法

    .net的dataset,datatable,object等对象转json方法(转)
    从anthom里面抽出.net部分数据专json的方法,包括特殊字符的转换,日期等js数据类型初始化:

    public class Converter
        
    {
            
    private static void WriteDataRow(StringBuilder sb, DataRow row)
            


            
    private static void WriteDataSet(StringBuilder sb, DataSet ds)
            


            
    private static void WriteDataTable(StringBuilder sb, DataTable table)
            


            
    private static void WriteEnumerable(StringBuilder sb, IEnumerable e)
            


            
    private static void WriteHashtable(StringBuilder sb, Hashtable e)
            


            
    private static void WriteObject(StringBuilder sb, object o)
            
    {
                MemberInfo[] members 
    = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
                sb.Append(
    "{");
                
    bool hasMembers = false;
                
    foreach (MemberInfo member in members)
                
    {
                    
    bool hasValue = false;
                    
    object val = null;
                    
    if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field)
                    
    {
                        FieldInfo field 
    = (FieldInfo)member;
                        val 
    = field.GetValue(o);
                        hasValue 
    = true;
                    }

                    
    else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property)
                    
    {
                        PropertyInfo property 
    = (PropertyInfo)member;
                        
    if (property.CanRead && property.GetIndexParameters().Length == 0)
                        
    {
                            val 
    = property.GetValue(o, null);
                            hasValue 
    = true;
                        }

                    }

                    
    if (hasValue)
                    
    {
                        sb.Append(
    "\"");
                        sb.Append(member.Name);
                        sb.Append(
    "\":");
                        WriteValue(sb, val);
                        sb.Append(
    ",");
                        hasMembers 
    = true;
                    }

                }

                
    if (hasMembers)
                
    {
                    
    --sb.Length;
                }

                sb.Append(
    "}");
            }


            
    private static void WriteString(StringBuilder sb, string s)
            

            
    public static void WriteValue(StringBuilder sb, object val)
            

            
    public static string Convert2Json(object o)
            
    {
                StringBuilder sb 
    = new StringBuilder();
                WriteValue(sb, o);
                
    return sb.ToString();
            }

        }
  • 相关阅读:
    有关javamelody的配置
    jsp页面路径问题
    mavne问题解决---Dynamic Web Module 2.3 or newer
    centos配置数据源和java环境配置
    iBatis之Iterator的使用
    [转]Android MediaPlayer状态机
    [转]Android Activity的加载模式和onActivityResult方法之间的冲突
    ubuntu1304无法启动桌面系统的问题和解决
    java使用AES256解密
    [转]理解Linux的处理器负载均值
  • 原文地址:https://www.cnblogs.com/newwind521/p/1208523.html
Copyright © 2011-2022 走看看