1. List<Map<String, Object>>多方式循环
package com.example.democrud.democurd.Prototype.demo01; import java.util.*; public class Demo5 { public static void main(String[] args) { //公共参数 List<Map<String, Object>> maps = ListmapTest(); //第一种方式 for循环 forTest(maps); //第二种方式 for迭代循环 iteratorTest(maps); entrySetTest(maps); } public static void entrySetTest(List<Map<String, Object>> maps) { /** * entrySet() 将原有Map集合中的键值作为一个整体返回Set集合 再将包含键值对对象的Set集合转化为Iterator接口对象, 然后获取集合中的所有的键值对映射关系, 在从映射关系中取出键和值 * */ for (Map<String, Object> map : maps) { Set<Map.Entry<String, Object>> entries = map.entrySet(); for (Map.Entry<String, Object> entry : entries) { String key = entry.getKey(); String value = entry.getValue().toString(); System.out.println("Key的值==》" + key); System.out.println("value的值==》" + value); } } System.out.println("-----------------分隔符---------entrySet()------↑↑↑↑↑↑↑------"); } /** * for迭代循环 * * @param maps */ public static void iteratorTest(List<Map<String, Object>> maps) { for (int i = 0; i < maps.size(); i++) { Map<String, Object> stringObjectMap = maps.get(i); System.out.println(stringObjectMap); Iterator<String> iterator = stringObjectMap.keySet().iterator(); while (iterator.hasNext()) { String next = iterator.next(); System.out.println("key=>" + next); String value = stringObjectMap.get(next).toString(); System.out.println("value=>" + value); } } System.out.println("-----------------分隔符---------for迭代循环----↑↑↑↑↑↑↑--------"); } /** * for循环 * * @param maps */ public static void forTest(List<Map<String, Object>> maps) { for (Map<String, Object> map : maps) { //KeySet(): 返回map中所有key值的列表。 for (String s : map.keySet()) { System.out.println("key的值 :" + s + "\t"); //根据对应的key获取到map的值 System.out.println(map.get(s)); } } System.out.println("-----------------分隔符--------for循环------↑↑↑↑↑↑↑-------"); } /** * 公共参数接口 * * @return */ public static List<Map<String, Object>> ListmapTest() { //List 中包含多个map List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); List<String> list1 = new ArrayList<String>(); List<String> list2 = new ArrayList<String>(); List<String> list3 = new ArrayList<String>(); List<String> list4 = new ArrayList<String>(); Map<String, Object> hashMap = new HashMap<String, Object>(); list1.add("我喜欢代丽1号"); list2.add("我喜欢代丽2号"); hashMap.put("小龙1", list1); hashMap.put("小龙2", list2); list.add(hashMap); Map<String, Object> hashMap1 = new HashMap<String, Object>(); list3.add("我喜欢代丽3号"); list4.add("我喜欢代丽4号"); hashMap1.put("小龙3", list3); hashMap1.put("小龙4", list4); list.add(hashMap1); System.out.println("公共参数为==>" + list); System.out.println("-----------------分隔符------公共接口--------↑↑↑↑↑↑↑-------"); return list; } }
测试结果
公共参数为==>[{小龙2=[我喜欢代丽2号], 小龙1=[我喜欢代丽1号]}, {小龙3=[我喜欢代丽3号], 小龙4=[我喜欢代丽4号]}] -----------------分隔符------公共接口--------↑↑↑↑↑↑↑------- key的值 :小龙2 [我喜欢代丽2号] key的值 :小龙1 [我喜欢代丽1号] key的值 :小龙3 [我喜欢代丽3号] key的值 :小龙4 [我喜欢代丽4号] -----------------分隔符--------for循环------↑↑↑↑↑↑↑------- {小龙2=[我喜欢代丽2号], 小龙1=[我喜欢代丽1号]} key=>小龙2 value=>[我喜欢代丽2号] key=>小龙1 value=>[我喜欢代丽1号] {小龙3=[我喜欢代丽3号], 小龙4=[我喜欢代丽4号]} key=>小龙3 value=>[我喜欢代丽3号] key=>小龙4 value=>[我喜欢代丽4号] -----------------分隔符---------for迭代循环----↑↑↑↑↑↑↑-------- Key的值==》小龙2 value的值==》[我喜欢代丽2号] Key的值==》小龙1 value的值==》[我喜欢代丽1号] Key的值==》小龙3 value的值==》[我喜欢代丽3号] Key的值==》小龙4 value的值==》[我喜欢代丽4号] -----------------分隔符---------entrySet()------↑↑↑↑↑↑↑------
2. Map<String,List<Map<String, Object>>>
package com.example.democrud.democurd.Prototype.demo01; import java.util.*; public class demo6 { //Map<String,List<Map<String, Object>>> public static void main(String[] args) { //原数据 Map<String, List<Map<String, Object>>> friend = friend(); //for循环 forTest(friend); System.out.println("\t"); iteratorTest(friend); } /** * @param friend */ public static void iteratorTest(Map<String, List<Map<String, Object>>> friend) { /** * map.entrySet().iterator(); * 首先是map.entrySet()是把HashMap类型的数据转换成集合类型; * 然后是获得map的迭代器,用作遍历map中的每一个键值对; * iterator()是去获得这个集合的迭代器,保存在iter里面; */ Iterator<Map.Entry<String, List<Map<String, Object>>>> iterator = friend.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, List<Map<String, Object>>> next = iterator.next(); System.out.println("获取到2租数据朋友和前女友的集合==>" + next); //上面现在已经map了 可以在迭代循环下 //扩展k,v 方式 String key = next.getKey(); String value = next.getValue().toString(); System.out.println("分别获取k,v的值==>" + key + "-*-" + value); //扩展结束 //已经拿到最外层的key 了我们需要继续拆分里面的v的值 可以看到他是list的集合 Iterator<Map<String, Object>> iterator1 = friend.get(key).iterator(); while (iterator1.hasNext()) { Map<String, Object> next1 = iterator1.next(); System.out.println(next1); Set<Map.Entry<String, Object>> entries = next1.entrySet(); for (Map.Entry<String, Object> entry : entries) { System.out.println("最后的数据==>" + entry); } } } System.out.println("\t"); System.out.println("-----------------分隔符--------iterator------↑↑↑↑↑↑↑-------"); } /** * for循环 * * @param friend */ public static void forTest(Map<String, List<Map<String, Object>>> friend) { //KeySet(): 返回map中所有key值的列表。 for (String s : friend.keySet()) { System.out.println("key值" + s); for (Map<String, Object> map : friend.get(s)) { System.out.println("value" + map); for (String s1 : map.keySet()) { String o = map.get(s1).toString(); System.out.println("==》key :" + s1 + "==》value :" + o); } } } System.out.println("\t"); System.out.println("-----------------分隔符--------for循环------↑↑↑↑↑↑↑-------"); } /** * 原始数据 * * @return */ public static Map<String, List<Map<String, Object>>> friend() { Map<String, Object> map = new HashMap<>(); Map<String, Object> map1 = new HashMap<>(); Map<String, Object> map2 = new HashMap<>(); Map<String, Object> map3 = new HashMap<>(); Map<String, Object> map4 = new HashMap<>(); //=后面new ArrayList<>() 取前面最外层的属性 List<Map<String, Object>> maps = new ArrayList<>(); List<Map<String, Object>> maps1 = new ArrayList<>(); //最外层数据 Map<String, List<Map<String, Object>>> yanwc = new HashMap<>(); map.put("name", "文超"); map.put("age", 25); map.put("gender", "男"); map1.put("name", "吊毛"); map1.put("age", 28); map1.put("gender", "男"); map2.put("name", "老黄"); map2.put("age", 26); map2.put("gender", "男"); map3.put("name", "代丽"); map3.put("age", 27); map3.put("gender", "女"); map4.put("name", "何晶"); map4.put("age", 27); map4.put("gender", "女"); maps.add(map); maps.add(map1); maps.add(map2); maps1.add(map3); maps1.add(map4); yanwc.put("friend", maps); yanwc.put("girlfriend", maps1); System.out.println("yanwc的数据==》" + yanwc); System.out.println("-----------------分隔符--------数据展示------↑↑↑↑↑↑↑-------"); return yanwc; } }
测试结果
yanwc的数据==》{girlfriend=[{gender=女, name=代丽, age=27}, {gender=女, name=何晶, age=27}], friend=[{gender=男, name=文超, age=25}, {gender=男, name=吊毛, age=28}, {gender=男, name=老黄, age=26}]} -----------------分隔符--------数据展示------↑↑↑↑↑↑↑------- key值girlfriend value{gender=女, name=代丽, age=27} ==》key :gender==》value :女 ==》key :name==》value :代丽 ==》key :age==》value :27 value{gender=女, name=何晶, age=27} ==》key :gender==》value :女 ==》key :name==》value :何晶 ==》key :age==》value :27 key值friend value{gender=男, name=文超, age=25} ==》key :gender==》value :男 ==》key :name==》value :文超 ==》key :age==》value :25 value{gender=男, name=吊毛, age=28} ==》key :gender==》value :男 ==》key :name==》value :吊毛 ==》key :age==》value :28 value{gender=男, name=老黄, age=26} ==》key :gender==》value :男 ==》key :name==》value :老黄 ==》key :age==》value :26 -----------------分隔符--------for循环------↑↑↑↑↑↑↑------- 获取到2租数据朋友和前女友的集合==>girlfriend=[{gender=女, name=代丽, age=27}, {gender=女, name=何晶, age=27}] 分别获取k,v的值==>girlfriend-*-[{gender=女, name=代丽, age=27}, {gender=女, name=何晶, age=27}] {gender=女, name=代丽, age=27} 最后的数据==>gender=女 最后的数据==>name=代丽 最后的数据==>age=27 {gender=女, name=何晶, age=27} 最后的数据==>gender=女 最后的数据==>name=何晶 最后的数据==>age=27 获取到2租数据朋友和前女友的集合==>friend=[{gender=男, name=文超, age=25}, {gender=男, name=吊毛, age=28}, {gender=男, name=老黄, age=26}] 分别获取k,v的值==>friend-*-[{gender=男, name=文超, age=25}, {gender=男, name=吊毛, age=28}, {gender=男, name=老黄, age=26}] {gender=男, name=文超, age=25} 最后的数据==>gender=男 最后的数据==>name=文超 最后的数据==>age=25 {gender=男, name=吊毛, age=28} 最后的数据==>gender=男 最后的数据==>name=吊毛 最后的数据==>age=28 {gender=男, name=老黄, age=26} 最后的数据==>gender=男 最后的数据==>name=老黄 最后的数据==>age=26 -----------------分隔符--------iterator------↑↑↑↑↑↑↑-------
3.实体类解套list多层
package com.example.democrud.democurd.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import java.util.List; public class LearnBase { public static void main(String[] args) { String ss = "{\"data\":[{\"packagemsg\":[{\"packagecode\":\"\",\"cardno\":\"1440902994877\",\"total\":307200,\"used\":32,\"allowance\":307168,\"ptype\":\"流量套餐\"},{\"packagecode\":\"\",\"cardno\":\"1440902994877\",\"total\":0,\"used\":0,\"allowance\":0,\"ptype\":\"短信套餐\"}],\"cardmsg\":{\"cardno\":\"1440902994877\",\"operatortypecode\":\"CMCC\",\"operatortype\":\"中国移动\",\"status\":\"正常\",\"saledate\":\"2020-12-30\",\"validdate\":\"2024-03-31\",\"activationdate\":\"2021-04-01\",\"peractivationdate\":\"2021-04-01\",\"cardaccount\":0,\"activestatus\":\"开机\"}},{\"packagemsg\":[{\"packagecode\":\"\",\"cardno\":\"1440629907054\",\"total\":307200,\"used\":7049,\"allowance\":300151,\"ptype\":\"流量套餐\"},{\"packagecode\":\"\",\"cardno\":\"1440629907054\",\"total\":0,\"used\":0,\"allowance\":0,\"ptype\":\"短信套餐\"}],\"cardmsg\":{\"cardno\":\"1440629907054\",\"operatortypecode\":\"CMCC\",\"operatortype\":\"中国移动\",\"status\":\"正常\",\"saledate\":\"2021-05-24\",\"validdate\":\"2024-06-30\",\"activationdate\":\"2021-07-01\",\"peractivationdate\":\"2021-07-01\",\"cardaccount\":0,\"activestatus\":\"关机\"}},{\"packagemsg\":[{\"packagecode\":\"\",\"cardno\":\"1440902994878\",\"total\":307200,\"used\":6913,\"allowance\":300287,\"ptype\":\"流量套餐\"},{\"packagecode\":\"\",\"cardno\":\"1440902994878\",\"total\":0,\"used\":0,\"allowance\":0,\"ptype\":\"短信套餐\"}],\"cardmsg\":{\"cardno\":\"1440902994878\",\"operatortypecode\":\"CMCC\",\"operatortype\":\"中国移动\",\"status\":\"正常\",\"saledate\":\"2020-12-30\",\"validdate\":\"2024-02-29\",\"activationdate\":\"2021-03-08\",\"peractivationdate\":\"2021-04-01\",\"cardaccount\":0,\"activestatus\":\"开机\"}}],\"resultmsg\":\"查询成功\",\"resultcode\":\"0\"}"; //解析最外层返回值 赋值于 ResultInfo 实体类 ResultInfo resultInfo = JSON.parseObject(ss, ResultInfo.class); // resultInfo.getData() 获取data数据进行遍历 resultInfo.getData().forEach(data ->{ //循环中在获取data的getPackagemsg遍历其中的数据 data.getPackagemsg().forEach(packageMsg -> { //获取data的getPackagemsg下的Cardno的值 System.out.println(packageMsg.getCardno()); }); }); } public static class CardMsg { private String cardno; private String operatortypecode; private String operatortype; private String status; private String saledate; private String validdate; private String activationdate; private String peractivationdate; private String cardaccount; private String activestatus; public String getCardno() { return cardno; } public void setCardno(String cardno) { this.cardno = cardno; } public String getOperatortypecode() { return operatortypecode; } public void setOperatortypecode(String operatortypecode) { this.operatortypecode = operatortypecode; } public String getOperatortype() { return operatortype; } public void setOperatortype(String operatortype) { this.operatortype = operatortype; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getSaledate() { return saledate; } public void setSaledate(String saledate) { this.saledate = saledate; } public String getValiddate() { return validdate; } public void setValiddate(String validdate) { this.validdate = validdate; } public String getActivationdate() { return activationdate; } public void setActivationdate(String activationdate) { this.activationdate = activationdate; } public String getPeractivationdate() { return peractivationdate; } public void setPeractivationdate(String peractivationdate) { this.peractivationdate = peractivationdate; } public String getCardaccount() { return cardaccount; } public void setCardaccount(String cardaccount) { this.cardaccount = cardaccount; } public String getActivestatus() { return activestatus; } public void setActivestatus(String activestatus) { this.activestatus = activestatus; } @Override public String toString() { return JSONObject.toJSONString(this, SerializerFeature.PrettyFormat); } } public static class PackageMsg { private int total; private String packagecode; private int allowance; private String ptype; private int used; private String cardno; public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public String getPackagecode() { return packagecode; } public void setPackagecode(String packagecode) { this.packagecode = packagecode; } public int getAllowance() { return allowance; } public void setAllowance(int allowance) { this.allowance = allowance; } public String getPtype() { return ptype; } public void setPtype(String ptype) { this.ptype = ptype; } public int getUsed() { return used; } public void setUsed(int used) { this.used = used; } public String getCardno() { return cardno; } public void setCardno(String cardno) { this.cardno = cardno; } @Override public String toString() { return JSONObject.toJSONString(this, SerializerFeature.PrettyFormat); } } public static class SimInfo { private List<PackageMsg> packagemsg; private CardMsg cardmsg; public List<PackageMsg> getPackagemsg() { return packagemsg; } public void setPackagemsg(List<PackageMsg> packagemsg) { this.packagemsg = packagemsg; } public CardMsg getCardmsg() { return cardmsg; } public void setCardmsg(CardMsg cardmsg) { this.cardmsg = cardmsg; } @Override public String toString() { return JSONObject.toJSONString(this, SerializerFeature.PrettyFormat); } } public static class ResultInfo { private List<SimInfo> data; private String resultmsg; private String resultcode; public List<SimInfo> getData() { return data; } public void setData(List<SimInfo> data) { this.data = data; } public String getResultmsg() { return resultmsg; } public void setResultmsg(String resultmsg) { this.resultmsg = resultmsg; } public String getResultcode() { return resultcode; } public void setResultcode(String resultcode) { this.resultcode = resultcode; } @Override public String toString() { return JSONObject.toJSONString(this, SerializerFeature.PrettyFormat); } } }
执行结果
1440902994877 1440629907054 1440629907054 1440902994878 1440902994878
1440902994877
分享结束;大家有不会或者不理解的可以留言提出;秒回哦;