C# Json格式化换行输出
public static string JsonBeauty(this string input, bool arrayWrap = false, string indents = " ") { void AppendIndent(StringBuilder sb, int count, string indents) { for (; count > 0; --count) sb.Append(indents); } var output = new StringBuilder(); int depth = 0; for (int i = 0; i < input.Length; ++i) { char ch = input[i]; if (ch == '\"') { bool str = true; while (str) { output.Append(ch); ch = input[++i]; if (ch == '\\') { output.Append(ch); ch = input[++i]; } else if (ch == '\"') str = false; } } switch (ch) { case '{': output.Append(ch); output.AppendLine(); AppendIndent(output, ++depth, indents); break; case '[': output.Append(ch); if (arrayWrap) { output.AppendLine(); AppendIndent(output, ++depth, indents); } break; case '}': output.AppendLine(); AppendIndent(output, --depth, indents); output.Append(ch); break; case ']': if (arrayWrap) { output.AppendLine(); AppendIndent(output, --depth, indents); } output.Append(ch); break; case ',': output.Append(ch); output.AppendLine(); AppendIndent(output, depth, indents); break; case ':': output.Append(" : "); break; default: if (!char.IsWhiteSpace(ch)) output.Append(ch); break; } } return output.ToString(); }
从FastJson提取出来的,