C# 转化成 json ,特殊字符的处理

//1.定义
string jsonText0= "{\"beijing\":{\"zkkke\":\"2222\",\"zppn\":\"1111\"}}";

//2.转化json格式

JavaScriptSerializer js = new JavaScriptSerializer();

js.Deserialize(jsonText0);

 

/// <summary>
/// 1.JSON字符串解析成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonStr">JSON字符串</param>
/// <returns></returns>
public static T GetObj<T>(string jsonStr)
{

T t = jsSerializer.Deserialize<T>(jsonStr);

return t;
}
#endregion

/// <summary>
/// json 对特殊字符的处理
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static String StringToJson(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 ‘\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;
default:
sb.Append(c);
break;
}
}
return sb.ToString();
}

C# 转化成 json ,特殊字符的处理

上一篇:C# POST请求


下一篇:三角形主机测评:一面艺术,一面科技