JSON
package script;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class JsonUtil {
public static Object get(String json, String code) {
// Get the JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String script = "var obj = " + json + ";";
try {
engine.eval(script);
{
return engine.eval("obj." + code);
}
} catch (ScriptException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
String json = "{'test':'this is test','test2':{'test3':'value3'}}";
{
Object value = JsonUtil.get(json, "test");
System.out.println(value);
}
{
Object value = JsonUtil.get(json, "test2.test3");
System.out.println(value);
}
}
}