Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Even simpler one that I just wrote:</p> <pre><code>public class JsonFormatter { public static string Indent = " "; public static string PrettyPrint(string input) { var output = new StringBuilder(input.Length * 2); char? quote = null; int depth = 0; for(int i=0; i&lt;input.Length; ++i) { char ch = input[i]; switch (ch) { case '{': case '[': output.Append(ch); if (!quote.HasValue) { output.AppendLine(); output.Append(Indent.Repeat(++depth)); } break; case '}': case ']': if (quote.HasValue) output.Append(ch); else { output.AppendLine(); output.Append(Indent.Repeat(--depth)); output.Append(ch); } break; case '"': case '\'': output.Append(ch); if (quote.HasValue) { if (!output.IsEscaped(i)) quote = null; } else quote = ch; break; case ',': output.Append(ch); if (!quote.HasValue) { output.AppendLine(); output.Append(Indent.Repeat(depth)); } break; case ':': if (quote.HasValue) output.Append(ch); else output.Append(" : "); break; default: if (quote.HasValue || !char.IsWhiteSpace(ch)) output.Append(ch); break; } } return output.ToString(); } } </code></pre> <p>Necessary extensions:</p> <pre><code> public static string Repeat(this string str, int count) { return new StringBuilder().Insert(0, str, count).ToString(); } public static bool IsEscaped(this string str, int index) { bool escaped = false; while (index &gt; 0 &amp;&amp; str[--index] == '\\') escaped = !escaped; return escaped; } public static bool IsEscaped(this StringBuilder str, int index) { return str.ToString().IsEscaped(index); } </code></pre> <p>Sample output:</p> <pre><code>{ "status" : "OK", "results" : [ { "types" : [ "locality", "political" ], "formatted_address" : "New York, NY, USA", "address_components" : [ { "long_name" : "New York", "short_name" : "New York", "types" : [ "locality", "political" ] }, { "long_name" : "New York", "short_name" : "New York", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] } ], "geometry" : { "location" : { "lat" : 40.7143528, "lng" : -74.0059731 }, "location_type" : "APPROXIMATE", "viewport" : { "southwest" : { "lat" : 40.5788964, "lng" : -74.2620919 }, "northeast" : { "lat" : 40.8495342, "lng" : -73.7498543 } }, "bounds" : { "southwest" : { "lat" : 40.4773990, "lng" : -74.2590900 }, "northeast" : { "lat" : 40.9175770, "lng" : -73.7002720 } } } } ] } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload