代码实例:
string json_data = "假装json数据";
// 压缩 json 数据
string json_data = JsonCompress(json_data);
封装代码:
private string JsonCompress(string json)
{
StringBuilder sb = new StringBuilder();
using (StringReader reader = new StringReader(json))
{
int ch = -1;
int lastch = -1;
bool isQuoteStart = false;
while ((ch = reader.Read()) > -1)
{
if ((char)lastch != '\\' && (char)ch == '\"')
{
if (!isQuoteStart)
{
isQuoteStart = true;
}
else
{
isQuoteStart = false;
}
}
if (!Char.IsWhiteSpace((char)ch) || isQuoteStart)
{
sb.Append((char)ch);
}
lastch = ch;
}
}
return sb.ToString();
}
网友评论