1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| 引用依赖 <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.0.7</version> </dependency>
import org.codehaus.commons.compiler.CompileException; import org.codehaus.janino.ScriptEvaluator; import java.lang.reflect.InvocationTargetException;
public class ScriptEvaluatorTest { public static void main(String[] args) throws CompileException, InvocationTargetException { String value = "\"{'data': [-1, 2358513858109449, 1, 'ffffffff-d066-4f4b-ffff-ffffc64e4518', '1db8248e-2ca9-4337-9c8e-9765d3b21a63', null, '337956e4408101f716aefab6b0b7b0c4', 'f8ffc8c37ce4', 'QC_Reference_Phone,Xiaomi,armeabi-v7a,santoni,Xiaomi,Redmi 4X,santoni', 1576759226316], 'createTime': 1576759226316}\""; ScriptEvaluator se = new ScriptEvaluator(); se.setReturnType(String.class); se.cook("import com.alibaba.fastjson.JSON;\n" + " import com.alibaba.fastjson.JSONArray;\n" + " import com.alibaba.fastjson.JSONObject;\n" + " System.out.println(" + value + ");" + " String valueData = " + value + ";\n" + " JSONObject jsonObject = JSON.parseObject(valueData);\n" + " JSONArray data = jsonObject.getJSONArray(\"data\");\n" + " String adjustId = (String) data.get(3);\n" + " return adjustId;"); Object evaluate = se.evaluate(new Object[]{}); System.out.println(evaluate.toString()); } }
|