2024-10-31 19:10:22
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("js");
String jsonstr = " \r\n [{}]";
try {
if (!jsonstr.matches("^[\r\n\f\t\\x20]*[\\{\\[][\\s\\S]*")) {
throw new ScriptException("必须以{或[开头");
}
se.eval("(" + jsonstr + ")");
System.out.println("是json格式");
} catch (ScriptException e) {
String exp = e.toString().replaceAll(".*\\:(.*)", "$1");
System.err.println(exp.replaceAll("\\(.+(?=at)", ""));
}
你好,感谢回答,但是我刚才测试了一下,好像只能识别一组kv,两组都会认为是错误,