美文网首页
C#:压缩 json 数据

C#:压缩 json 数据

作者: 白祤星 | 来源:发表于2020-03-20 14:20 被阅读0次

    代码实例:


    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();
            }
    

    相关文章

      网友评论

          本文标题:C#:压缩 json 数据

          本文链接:https://www.haomeiwen.com/subject/drjjyhtx.html