请稍等ManixChen正在解析过程中………



Java中用js解析json



在java中如何解析json?fastjson?jackson?那未免太无趣了

其实我们可以试试ScriptEngine

public class NashornTest {

    private static String json = "[{name:'A',age:'18'},{name:'B',age:'19'},{name:'C',age:'30'}]";

    private static String script =
            "function parse(json){" +
            "   var names = new Array();" +
            "   for(var i in json){" +
            "       names.push(json[i].name);" +
            "   }" +
            "   return names;" +
            "};" +
            "parse(" + json + ");";

    public static void main(String[] args) throws ScriptException {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName( "JavaScript" );

        Map<String,String> result = (Map<String,String>)engine.eval(script);
        result.forEach((k,v) -> System.out.println(v));
    }
}

输出:

A

B

C

ScriptEngineManager

Java

javascript

javascript 脚手架制作

javascript webpack配置

javascript webpack配置

javascript开启网页录制功能ES6语法 async

javascript ES6重点语法记录

javascript vue3 props 验证属性

javascript webpack经常用的插件包

javascript vite构建工具入门到上手

javascript vue3 props 验证属性

javascript lodashjs常见对象的所有操作

RMI

Servlet乱码分析

spring security 探秘

Bean Validation

SQL 拼接

java ScriptEngineManager Java javascript Feb 22, 2015