C# 中处理 JSON-字符串属性值转义

作者:vkvi 来源:ITPOW(原创) 日期:2022-1-12

JSON 字符串属性值通常用双引号引起来的,所以属性值中如果出现换行、双引号,就会导致 JSON 格式出错,所以需要转换,比如下面这个方法:

public static string Escape(string propValue)
{
	if (string.IsNullOrEmpty(propValue))
	{
		return "";
	}

	return propValue.Replace("\\", "\\\\")
		.Replace("\"", "\\\"")
		.Replace("\'", "\\\'")
		.Replace("\r", "\\u000d")
		.Replace("\n", "\\u000a");
}


相关文章