表达式解析-beanpath
由来
很多javabean嵌套着很多层对象,这其中还夹杂着map、collection等对象,因此获取太深的嵌套对象会让代码变得冗长不堪。因此我们可以考虑使用一种表达式来获取指定深度的对象,于是beanresolver应运而生。
原理
通过传入一个表达式,按照表达式的规则获取bean中指定的字段值。
表达式分为两种:
- .表达式,可以获取bean对象中的属性(字段)值或者map中key对应的值
- []表达式,可以获取集合等对象中对应index的值
栗子:
- person 获取bean对象下person字段的值,或者bean本身如果是person对象,返回本身。
- person.name 获取bean中person字段下name字段的值,或者bean本身如果是person对象,返回其name字段的值。
- persons[3] 获取persons字段下第三个元素的值(假设person是数组或collection对象)
- person.friends[5].name 获取person字段下friends列表(或数组)的第5个元素对象的name属性
使用
由于嵌套bean定义过于复杂,在此我们省略,有兴趣的可以看下这里:cn.hutool.core.lang.test.bean(src/test/java下)下定义了测试用例用的bean。
首先我们创建这个复杂的bean(实际当中这个复杂的bean可能是从数据库中获取,或者从json转入)
这个复杂bean的关系是这样的:
定义一个map包含用户信息(userinfodict)和一个标志位(flag),用户信息包括一些基本信息和一个考试信息列表(examinfodict)。
//------------------------------------------------- 考试信息列表 examinfodict examinfodict = new examinfodict(); examinfodict.setid(1); examinfodict.setexamtype(0); examinfodict.setansweris(1); examinfodict examinfodict1 = new examinfodict(); examinfodict1.setid(2); examinfodict1.setexamtype(0); examinfodict1.setansweris(0); examinfodict examinfodict2 = new examinfodict(); examinfodict2.setid(3); examinfodict2.setexamtype(1); examinfodict2.setansweris(0); list<examinfodict> examinfodicts = new arraylist<examinfodict>(); examinfodicts.add(examinfodict); examinfodicts.add(examinfodict1); examinfodicts.add(examinfodict2); //------------------------------------------------- 用户信息 userinfodict userinfodict = new userinfodict(); userinfodict.setid(1); userinfodict.setphotopath("yx.mm.com"); userinfodict.setrealname("张三"); userinfodict.setexaminfodict(examinfodicts); map<string, object> tempmap = new hashmap<string, object>(); tempmap.put("userinfo", userinfodict); tempmap.put("flag", 1); 下面,我们使用beanpath获取这个map下此用户第一门考试的id: beanpath resolver = new beanpath("userinfo.examinfodict[0].id"); object result = resolver.get(tempmap);//id为1 只需两句(甚至一句)即可完成复杂bean中各层次对象的获取。
说明: 为了简化beanpath的使用,hutool在beanutil中也加入了快捷入口方法:beanutil.getproperty,这个方法的命名更容易理解(毕竟beanpath不但可以解析bean,而且可以解析map和集合)。
例子
上面的文章是从糊涂官网上直接截取的。缺少了设置值的过程https://doc.hutool.cn/pages/beanpath/#%e5%8e%9f%e7%90%86
下面我自己尝试的一些使用示例,供大家参考
map<string, object> map = new hashmap<>(); map.put("name", "张三"); map.put("age", 18); map<string, object> score = new hashmap<>(); score.put("math", 100); score.put("english", 99); list<string> likefood = new arraylist<>(); likefood.add("apple"); likefood.add("banana"); map.put("score", score); map.put("likefood", likefood); beanpath matchscorepath = beanpath.create("score.math"); beanpath likefoodpath = beanpath.create("likefood[1]"); // 输出结果 system.out.println("matchscore"+matchscorepath.get(map)); system.out.println("likefood"+likefoodpath.get(map)); // 设置值 matchscorepath.set(map, 90); likefoodpath.set(map, "orange"); system.out.println(((map<string,object>)map.get("score")).get("math")); system.out.println(((list)map.get("likefood")).get(1)); // 设置不存在的值 beanpath likefoodpath1 = beanpath.create("likefood[2]"); likefoodpath1.set(map, "pear"); system.out.println(((list)map.get("likefood")).get(2));
到此这篇关于java根据表达式获取对象中的值,设置值的文章就介绍到这了,更多相关java获取对象中的值内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论