using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json.Linq; namespace Sog { public class JsonUtils { private static string TrimSpace(string json) { int i = 0, j = 0; while (json[i] != '\0') { if (json[i] == ' ') { json.Replace(json[j++], json[i]); } i++; } return json; } private static string indent(int num) { string result = ""; for (int i = 0; i < num; i++) { result += " "; } return result; } public static string FormatJson(string json) { json.Trim(' '); string result = ""; int length = json.Length; int number = 0; char key; for (int i = 0; i < length; i++) { key = json[i]; if (key == '[' || key == '{') { if ((i - 1) > 0 && json[i - 1] == ':') { result += '\n'; result += indent(number); } result += key; result += '\n'; number++; result += indent(number); continue; } if (key == ']' || key == '}') { if ((i - 1 >= 0) && (json[i - 1] == ']' || json[i - 1] == '}')) { number--; result += indent(number); result += key; } else { result += '\n'; number--; result += indent(number); result += key; } if (i + 1 < length && json[i + 1] != ',') { result += '\n'; } continue; } if (key == ',') { result += key; result += '\n'; result += indent(number); continue; } result += key; } return result; } } }