I’ve been working with JSON in the new Xojo framework quite a bit lately and wanted the ability to format the JSON text so it is more readable.
A little Internet research turned up a set of classes for .NET (in C#) that can do this. I took a few minutes to port these three classes over to a single Xojo class that can format JSON for you.
To use the class, add it to your project and then add code like this:
Dim formatter As New JSONFormatter(json) Dim formattedJSON As Text = formatter.Format
Here is an example of unformatted JSON:
{"Crows":{"players":{"Ben":{"position":"1B"},"Ty":{"position":"2B"}}},"Pigeons":{"players":{"Bill":{"position":"1B"},"Tim":{"position":"2B"}}},"Seagulls":{"players":{"Bob":{"position":"1B"},"Tom":{"position":"2B"}}}}
And what it looks like after being formatted by JSONFormatter:
{ "Crows": { "players": { "Ben": { "position":"1B" }, "Ty": { "position":"2B" } } }, "Pigeons": { "players": { "Bill": { "position":"1B" }, "Tim": { "position":"2B" } } }, "Seagulls": { "players": { "Bob": { "position":"1B" }, "Tom": { "position":"2B" } } } }
I hope you find this useful. Want to learn more about using JSON with Xojo? Watch our on-demand JSON webinar, learn to use JSON with Xojo along with slides and a sample project.