I’m writing some REST WebServices that return JSON and wanted to ensure that my JSON was correctly formatted and parsable. Since the JDK ships with a JavaScript engine built into it now, I figured it would be simplest to just use that and avoid any external dependencies. Here’s my code:
ScriptEngine se = new ScriptEngineManager().getEngineByExtension("js"); se.eval("var obj = " + json + ";");
Pretty simple overall. The only tricky part is that you need to add the “var obj = ” and the semicolon on the end so that the JavaScript engine can evaluate it correctly.
This is useful, thanks!
LikeLike