JasonHelper.Escape 转换字符串为jason格式代码

JasonHelper
public static class JasonHelper
{
    
public static string Escape(string s)
    {
        
if (s == null)
            
return null;
        StringBuilder sb 
= new StringBuilder();
        
for (int i = 0; i < s.Length; i++)
        {
            
char ch = s[i];
            
switch (ch)
            {
                
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");
                    
break;
                
case '\r':
                    sb.Append(
"\\r");
                    
break;
                
case '\t':
                    sb.Append(
"\\t");
                    
break;
                
case '/':
                    sb.Append(
"\\/");
                    
break;
                
default:
                    sb.Append(ch);
                    
break;
            }
        }
//for 
        return sb.ToString();
    } 
}
posted @ 2009-07-20 14:20  玉开  阅读(1051)  评论(2编辑  收藏  举报