• .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();
            }

        }
  • 相关阅读:
    正则基础之——反向引用
    zend framework 配置连接数据库
    Zend Framework Module之多模块配置
    zend framework 1.10项目配置与经典hello world
    CSS3 @font-face
    js跳转页面方法(转)
    CSS中怎么让DIV居中
    HTML段落自动换行的样式设置
    PHPEXCEL实例-导出EXCEL
    移动端框架篇-控制父容器的滑屏框架-slip.js
  • 原文地址:https://www.cnblogs.com/newwind521/p/1208523.html
Copyright © 2020-2023  润新知