浙江省高等学校教师教育理论培训

微信搜索“毛凌志岗前心得”小程序

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

reference:

here is an example

            String promotion_price = null;
            String json_string="var shopVipData={\"msg\":\"\",promo:[{\"type\":\"普通会员\",\"price\":\"45.75\"},{\"type\":\"高级会员\",\"price\":\"45.71\"},{\"type\":\"VIP会员\",\"price\":\"45.34\"},{\"type\":\"至尊VIP\",\"price\":\"44.88\"}]};";
            json_string=http_response;
            logger.debug(json_string);
            String js_string="var price;for(var i=0;i<=shopVipData.promo.length-1;i++){if(shopVipData.promo[i].type=='普通会员'){price=shopVipData.promo[i].price;}}";

            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("JavaScript");
            Compilable compilable = (Compilable) engine;
            CompiledScript script = null;
            Bindings binds = engine.createBindings();
            try {
                script = compilable.compile(json_string+js_string);
                script.eval(binds);
                promotion_price=binds.get("price").toString();
                logger.debug("=-----------------------------");
               
            } catch (ScriptException e) {
                e.printStackTrace();
            }   

posted on 2012-02-03 00:27  lexus  阅读(429)  评论(0编辑  收藏  举报