关于下面的java脚本,我想读取java类中的employees变量。你能帮帮我吗?
var employees = {
accounting: []
};
for(var i in someData) {
var item = someData[i];
employees.accounting.push({
"firstName" : item.firstName,
"lastName" : item.lastName,
"age" : item.age
});
}
答案 0 :(得分:0)
如果您使用的是java6或java7 你可以在java中获取javascript变量,如下所示:
(在java8中删除了NativeObject,但是你可以使用类似的类[jdk.nashorn.api.scripting.ScriptObjectMirror])
public class Test5 {
private static final String TEST_SCRIPT =
"var employees = {"
+ " accounting: []"
+ "};"
+ "for(i=1; i<=3; i++) {"
+ " employees.accounting.push({ "
+ " \"firstName\" : \"firstName\" + i,"
+ " \"lastName\" : \"lastName\" + i,"
+ " \"age\" : \"age\" + i"
+ "});"
+ "}"
+ "employees;";
/**
* @param args
*/
public static void main(String[] args) {
try{
CompiledScript script = compEngine.compile(TEST_SCRIPT);
NativeObject obj = (NativeObject) script.eval();
NativeArray obj1 = (NativeArray) obj.get(obj.getAllIds()[0]);
for (Object temp : obj1) {
NativeObject obj2 = (NativeObject) temp;
System.out.println(obj2.get("firstName"));
System.out.println(obj2.get("lastName"));
System.out.println(obj2.get("age"));
}
}catch(Exception e){e.printStackTrace();}
}
}