text 来自https://stackoverflow.com/questions/5881204/how-to-set-formatting-with-javascriptserializer-w

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 来自https://stackoverflow.com/questions/5881204/how-to-set-formatting-with-javascriptserializer-w相关的知识,希望对你有一定的参考价值。

/// <summary>
/// Adds indentation and line breaks to output of JavaScriptSerializer
/// </summary>
public static string FormatOutput(string jsonString)
{
    var stringBuilder = new StringBuilder();

    bool escaping = false;
    bool inQuotes = false;
    int indentation = 0;

    foreach (char character in jsonString)
    {
        if (escaping)
        {
            escaping = false;
            stringBuilder.Append(character);
        }
        else
        {
            if (character == '\\')
            {
                escaping = true;
                stringBuilder.Append(character);
            }
            else if (character == '\"')
            {
                inQuotes = !inQuotes;
                stringBuilder.Append(character);
            }
            else if (!inQuotes)
            {
                if (character == ',')
                {
                    stringBuilder.Append(character);
                    stringBuilder.Append("\r\n");
                    stringBuilder.Append('\t', indentation);
                }
                else if (character == '[' || character == '{')
                {
                    stringBuilder.Append(character);
                    stringBuilder.Append("\r\n");
                    stringBuilder.Append('\t', ++indentation);
                }
                else if (character == ']' || character == '}')
                {
                    stringBuilder.Append("\r\n");
                    stringBuilder.Append('\t', --indentation);
                    stringBuilder.Append(character);
                }
                else if (character == ':')
                {
                    stringBuilder.Append(character);
                    stringBuilder.Append('\t');
                }
                else
                {
                    stringBuilder.Append(character);
                }
            }
            else
            {
                stringBuilder.Append(character);
            }
        }
    }

    return stringBuilder.ToString();
}

以上是关于text 来自https://stackoverflow.com/questions/5881204/how-to-set-formatting-with-javascriptserializer-w的主要内容,如果未能解决你的问题,请参考以下文章

来自 watchOS 中 if 条件 Text() 的 SwiftUI 运行时错误

text 来自ip的Wget

text 来自apto列表的回复

text 来自Adam的测试片段

text BM1682支持来自Caffe算子

text 来自cli的mysql查询