So, I'm currently playing around with some simple json and its parsing into a collection structure - and just did my usual System.out.println() as debug - and noticed the output to be very similar to the JSON input.
Start of original JSON input:
Output of my System.out.println(List<Map<String, String>>) (new lines added to avoid extreme long line - it's actually one long string without line breaks):
So, the output of List.toString() combined with Map.toString() pretty much resembles the JSON input - although all keys and values missing their quotes and the colon is replaced with an equals - but aside from these minor details which can be done with simple String.replace(String, String) the question came up in my head:
If the toString() methods are pretty much output almost-JSON'ish strings - are there any built in parsers doing the same in reverse without the need for use a JSON-lib or some hacky RegEx?
From earlier experiments with OAuth I know that RegEx isn't the best to parse JSON - although it's sufficient for "simple" structures as OAuth or this simple file list I'm dealing with.
Has anyone else already noticed this behaviour and maybe know more about this?
As a side-node: If someone is to suggest using JSON-lib to me: No issues doing that - but I would appreciate using a slim one without a lots of dependencies, as some of linked ones on the json.org site require.
Even though the output of a list's or map's toString looks like JSON, I never would use any string manipulation to turn it into proper JSON. You can't just replace = with :, add quotes, etc. What if your keys or values contain quotes, = signs, commas, etc? For similar reasons you should never use regular expressions for parsing complex structures like JSON, XML or HTML. Regular expressions simply can't handle those structures - the nesting alone is going to be impossible to handle properly.
That said: there are no built-in parsers for JSON. There are for XML, in the java.xml module, but not JSON. However, there are plenty of good libraries available. The ones I've used:
Thanks for your reply.
As for a few reasons it doesn't really matter any longer, but as for the libs you linked only googles Gson seems the one to go, as I struggle to find a way to download jackson and json-p seems EE stuff.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop