JSON 字符串属性值通常用双引号引起来的,所以属性值中如果出现换行、双引号,就会导致 JSON 格式出错,所以需要转换,比如下面这个方法:
public static string Escape(string propValue) { if (string.IsNullOrEmpty(propValue)) { return ""; } return propValue.Replace("\\", "\\\\") .Replace("\"", "\\\"") .Replace("\'", "\\\'") .Replace("\r", "\\u000d") .Replace("\n", "\\u000a"); }