java 通过 list- 实现 json 生成
博客分类: javajson java实现json拼接
程序员文章站
2024-02-12 08:35:46
...
java 实现 json 拼接
功能:通过 item 添加数据的方式 ,解析拼接成 json 字符串
待优化项: 使用 jsonobject.put(name,value) 而并非 map.put(name,value)
结果展示
[ {"gId":0,"id":1,"name":"config_latest_version","pId":0,"type":1,"value":"1.0.43"}, {"gId":0,"id":2,"name":"app_info","pId":0,"type":2}, {"gId":0,"id":3,"name":"app_latest_version","pId":2,"type":1,"value":"3.1.44"}, {"gId":0,"id":4,"name":"app_version_illustration","pId":2,"type":1,"value":"www.baoyou.com android版本3.1.44"}, {"gId":0,"id":5,"name":"country_code","pId":0,"type":3}, {"gId":1,"id":6,"name":"code","pId":5,"type":1,"value":"1"}, {"gId":1,"id":7,"name":"value","pId":5,"type":1,"value":"+86"}, {"gId":1,"id":8,"name":"sequence","pId":5,"type":1,"value":"1"}, {"gId":0,"id":9,"name":"slide_show_list","pId":0,"type":3}, {"gId":1,"id":10,"name":"type","pId":9,"type":1,"value":"slide_show_001"}, {"gId":1,"id":11,"name":"effective_start_time","pId":9,"type":1,"value":"2016-09-06 08:00:00"}, {"gId":1,"id":12,"name":"effective_end_time","pId":9,"type":1,"value":"2016-09-06 08:00:00"}, {"gId":1,"id":13,"name":"times","pId":9,"type":1,"value":"3"}, {"gId":1,"id":14,"name":"sequence","pId":9,"type":1,"value":"1"}, {"gId":1,"id":15,"name":"picture_list","pId":9,"type":3}, {"gId":1,"id":16,"name":"code","pId":15,"type":1,"value":"1"}, {"gId":1,"id":17,"name":"vale","pId":15,"type":1,"value":"https://www.baoyou.com/function_introduction/1.png?version=2016930"}, {"gId":1,"id":18,"name":"url","pId":15,"type":1,"value":""}, {"gId":1,"id":19,"name":"sequence","pId":15,"type":1,"value":"1"}, {"gId":2,"id":20,"name":"code","pId":15,"type":1,"value":"2"}, {"gId":2,"id":21,"name":"vale","pId":15,"type":1,"value":"https://www.baoyou.com/function_introduction/2.png?version=2016930"}, {"gId":2,"id":22,"name":"url","pId":15,"type":1,"value":""}, {"gId":2,"id":23,"name":"sequence","pId":15,"type":1,"value":"2"}, {"gId":3,"id":24,"name":"code","pId":15,"type":1,"value":"3"}, {"gId":3,"id":25,"name":"vale","pId":15,"type":1,"value":"https://www.baoyou.com/function_introduction/3.png?version=20161129"}, {"gId":3,"id":26,"name":"url","pId":15,"type":1,"value":""}, {"gId":3,"id":27,"name":"sequence","pId":15,"type":1,"value":"3"}, {"gId":2,"id":28,"name":"type","pId":9,"type":1,"value":"slide_show_002"}, {"gId":2,"id":29,"name":"effective_start_time","pId":9,"type":1,"value":"2016-09-06 08:00:00"}, {"gId":2,"id":30,"name":"effective_end_time","pId":9,"type":1,"value":"2016-09-06 08:00:00"}, {"gId":2,"id":31,"name":"times","pId":9,"type":1,"value":"3"}, {"gId":2,"id":32,"name":"sequence","pId":9,"type":1,"value":"2"}, {"gId":2,"id":33,"name":"picture_list","pId":9,"type":3}, {"gId":1,"id":34,"name":"code","pId":33,"type":1,"value":"1"}, {"gId":1,"id":35,"name":"vale","pId":33,"type":1,"value":"https://www.baoyou.com/function_introduction/1.png?version=2016930"}, {"gId":1,"id":36,"name":"url","pId":33,"type":1,"value":""}, {"gId":1,"id":37,"name":"sequence","pId":33,"type":1,"value":"1"}, {"gId":2,"id":38,"name":"code","pId":33,"type":1,"value":"2"}, {"gId":2,"id":39,"name":"vale","pId":33,"type":1,"value":"https://www.baoyou.com/function_introduction/2.png?version=2016930"}, {"gId":2,"id":40,"name":"url","pId":33,"type":1,"value":""}, {"gId":2,"id":41,"name":"sequence","pId":33,"type":1,"value":"2"} ]
{ "app_info": { "app_latest_version": "3.1.44", "app_version_illustration": "www.baoyou.com android版本3.1.44" }, "config_latest_version": "1.0.43", "country_code": [ { "code": "1", "sequence": "1", "value": "+86" } ], "slide_show_list": [ { "effective_end_time": "2016-09-06 08:00:00", "effective_start_time": "2016-09-06 08:00:00", "picture_list": [ { "code": "3", "sequence": "3", "url": "", "vale": "https://www.baoyou.com/function_introduction/3.png?version=20161129" }, { "code": "2", "sequence": "2", "url": "", "vale": "https://www.baoyou.com/function_introduction/2.png?version=2016930" }, { "code": "1", "sequence": "1", "url": "", "vale": "https://www.baoyou.com/function_introduction/1.png?version=2016930" } ], "sequence": "1", "times": "3", "type": "slide_show_001" }, { "effective_end_time": "2016-09-06 08:00:00", "effective_start_time": "2016-09-06 08:00:00", "picture_list": [ { "code": "2", "sequence": "2", "url": "", "vale": "https://www.baoyou.com/function_introduction/2.png?version=2016930" }, { "code": "1", "sequence": "1", "url": "", "vale": "https://www.baoyou.com/function_introduction/1.png?version=2016930" } ], "sequence": "2", "times": "3", "type": "slide_show_002" } ] }
代码部分:
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface TreeNodeGId { }
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface TreeNodeId { }
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.google.common.collect.Lists; import www.baoyou.com.annotation.TreeNodeGId; import www.baoyou.com.annotation.TreeNodeId; import www.baoyou.com.annotation.TreeNodeName; import www.baoyou.com.annotation.TreeNodePId; import www.baoyou.com.annotation.TreeNodeType; import www.baoyou.com.annotation.TreeNodeValue; import www.baoyou.com.util.node.BuildHelper; import www.baoyou.com.util.node.JsonHelper; public class Item { /* [ { id:1, pId:0,gid:null,type:"1", name:"config_latest_version", open:true,value:"1.0.43"}, { id:2, pId:0,gid:null,type:"2", name:"app_info", open:true}, { id:3, pId:2,gid:null,type:"1", name:"app_latest_version",value:"3.1.44"}, { id:4, pId:2,gid:null,type:"1", name:"app_version_illustration",value:"www.baoyou.com android版本3.1.44"}, { id:5, pId:0,gid:null,type:"3", name:"country_code"}, { id:6, pId:5,gid:1,type:"1", name:"code",value:"1"}, { id:7, pId:5,gid:1,type:"1", name:"value",value:"+86"}, { id:8, pId:5,gid:1,type:"1", name:"sequence",value:"1"} { id:9, pId:0,gid:0,type:"3", name:"slide_show_list"}, { id:10, pId:9,gid:1,type:"3", name:"type",value:"slide_show_001"}, { id:11, pId:9,gid:1,type:"3", name:"effective_start_time",value:"2016-09-06 08:00:00"}, { id:12, pId:9,gid:1,type:"3", name:"effective_end_time",value:"2016-12-06 08:00:00"}, { id:13, pId:9,gid:1,type:"3", name:"times",value:"3"}, { id:14, pId:9,gid:1,type:"3", name:"sequence",value:"1"}, { id:15, pId:9,gid:1,type:"3", name:"picture_list"}, { id:16, pId:15,gid:1,type:"3", name:"code",value:"1"}, { id:17, pId:15,gid:1,type:"3", name:"vale",value:"https://www.baoyou.com/1.png?version=2016930"}, { id:18, pId:15,gid:1,type:"3", name:"url",value:""}, { id:19, pId:15,gid:1,type:"3", name:"sequence",value:"1"}, { id:20, pId:15,gid:2,type:"3", name:"code",value:"2"}, { id:21, pId:15,gid:2,type:"3", name:"vale",value:"https://www.baoyou.com/2.png?version=2016930"}, { id:22, pId:15,gid:2,type:"3", name:"url",value:""}, { id:23, pId:15,gid:2,type:"3", name:"sequence",value:"2"}, { id:24, pId:15,gid:3,type:"3", name:"code",value:"3"}, { id:25, pId:15,gid:3,type:"3", name:"vale",value:"https://www.baoyou.com/3.png?version=20161129"}, { id:26, pId:15,gid:3,type:"3", name:"url",value:""}, { id:27, pId:15,gid:3,type:"3", name:"sequence",value:"3"} { id:28, pId:9,gid:2,type:"3", name:"type",value:"slide_show_002"}, { id:29, pId:9,gid:2,type:"3", name:"effective_start_time",value:"2016-09-06 08:00:00"}, { id:30, pId:9,gid:2,type:"3", name:"effective_end_time",value:"2016-12-06 08:00:00"}, { id:31, pId:9,gid:2,type:"3", name:"times",value:"3"}, { id:32, pId:9,gid:2,type:"3", name:"sequence",value:"1"}, { id:33, pId:9,gid:2,type:"3", name:"picture_list"}, { id:34, pId:33,gid:1,type:"3", name:"code",value:"4"}, { id:35, pId:33,gid:1,type:"3", name:"vale",value:"https://www.baoyou.com/carouselfigure02.png?version=2016930"}, { id:36, pId:33,gid:1,type:"3", name:"url",value:""}, { id:37, pId:33,gid:1,type:"3", name:"sequence",value:"4"}, { id:38, pId:33,gid:2,type:"3", name:"code",value:"5"}, { id:39, pId:33,gid:2,type:"3", name:"vale",value:"https://www.baoyou.com/carouselfigure02.png?version=2016930"}, { id:40, pId:33,gid:2,type:"3", name:"url",value:""}, { id:41, pId:33,gid:2,type:"3", name:"sequence",value:"5"} ] */ public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException { // Item(int id, int pId, int type, int gId, String name, String value) Item item1 = new Item(1,0,1,0,"config_latest_version","1.0.43"); Item item2 = new Item(2,0,2,0,"app_info",null); Item item3 = new Item(3,2,1,0,"app_latest_version","3.1.44"); Item item4 = new Item(4,2,1,0,"app_version_illustration","android版本3.1.44"); Item item5 = new Item(5,0,3,0,"country_code",null); Item item6 = new Item(6,5,1,1,"code","1"); Item item7 = new Item(7,5,1,1,"value","+86"); Item item8 = new Item(8,5,1,1,"sequence","1"); Item item9 = new Item(9,5,1,2,"code","2"); Item item10 = new Item(10,5,1,2,"value","+001"); Item item11 = new Item(11,5,1,2,"sequence","2"); Map map = new TreeMap<String,String>(); map.put(item1.name, item1.value); Map map2 = new TreeMap<String,String>(); map2.put(item3.name, item3.value); map2.put(item4.name, item4.value); map.put(item2.name, map2); Map map6 = new TreeMap<String,String>(); map6.put(item6.name, item6.value); map6.put(item7.name, item7.value); map6.put(item8.name, item8.value); List list = new ArrayList<Map>(); list.add(map6); map.put(item5.name,list); System.out.println(JSON.toJSONString(map2)); System.out.println(JSON.toJSONString(map)); List <Item> original = Lists.newArrayList(item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,item11); List<Node> convertDatas2Nodes = BuildHelper.convertDatas2Nodes(original); List<Node> sortedNodes = BuildHelper.getSortedNodes(convertDatas2Nodes); Map result = JsonHelper.getResult(sortedNodes); System.out.println(JSON.toJSONString(result,SerializerFeature.DisableCircularReferenceDetect)); } @TreeNodeId public int id = 0; @TreeNodePId public int pId = 0; @TreeNodeType public int type = 1; // 1:k-v 2:k-o 3:k-l @TreeNodeGId public int gId = 0; // 3 存在 1 null 2 null @TreeNodeName public String name = null; @TreeNodeValue public String value = null; // 1 存在 2 null 3 null public Item(int id, int pId, int type, int gId, String name, String value) { this.id = id; this.pId = pId; this.type = type; this.gId = gId; this.name = name; this.value = value; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getpId() { return pId; } public void setpId(int pId) { this.pId = pId; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getgId() { return gId; } public void setgId(int gId) { this.gId = gId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @Override public String toString() { return "Item [name=" + name + ", value=" + value + "]"; } }
import java.util.ArrayList; import java.util.List; import www.baoyou.com.annotation.TreeNodeGId; import www.baoyou.com.annotation.TreeNodeId; import www.baoyou.com.annotation.TreeNodeName; import www.baoyou.com.annotation.TreeNodePId; import www.baoyou.com.annotation.TreeNodeType; import www.baoyou.com.annotation.TreeNodeValue; public class Node { @TreeNodeId public int id = 0; @TreeNodePId public int pId = 0; @TreeNodeType public int type = 1; // 1:k-v 2:k-o 3:k-l @TreeNodeGId public int gId = 0; // 3 存在 1 null 2 null @TreeNodeName public String name = null; @TreeNodeValue public String value = null; // 1 存在 2 null 3 null public int level; public boolean isLeaf; private Node parent; public List<Node> child= new ArrayList<Node>(); public Node(){} public Node(int id, int pId, int type, int gId, String name, String value) { this.id = id; this.pId = pId; this.type = type; this.gId = gId; this.name = name; this.value = value; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getpId() { return pId; } public void setpId(int pId) { this.pId = pId; } public int getType() { return type; } public void setType(int type) { this.type = type; } public int getgId() { return gId; } public void setgId(int gId) { this.gId = gId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public int getLevel() { return parent == null ? 1 : parent.getLevel() + 1; } public void setLevel(int level) { this.level = level; } public boolean isLeaf() { return this.child.size() == 0; } public List<Node> getChild() { return child; } public void setChild(List<Node> child) { this.child = child; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public boolean isRoot(){ return this.parent == null; } }
import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.TreeMap; import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; import www.baoyou.com.annotation.TreeNodeGId; import www.baoyou.com.annotation.TreeNodeId; import www.baoyou.com.annotation.TreeNodeName; import www.baoyou.com.annotation.TreeNodePId; import www.baoyou.com.annotation.TreeNodeType; import www.baoyou.com.annotation.TreeNodeValue; import www.baoyou.com.entity.Item; import www.baoyou.com.entity.Node; public class BuildHelper { @SuppressWarnings("rawtypes") public static <T> List<Node> convertDatas2Nodes(List<T> datas) throws IllegalArgumentException, IllegalAccessException { List<Node> nodes = new ArrayList<Node>(); Node node = null; for (T t : datas) { int id = 0; int pId = 0; int type = 1; // 1:k-v 2:k-o 3:k-l int gId = 0; // 3 存在 1 null 2 null String name = null; String value= null; node = new Node(); Class clazz = t.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.getAnnotation(TreeNodeId.class) != null) { field.setAccessible(true); id = field.getInt(t); } if (field.getAnnotation(TreeNodePId.class) != null) { field.setAccessible(true); pId = field.getInt(t); } if (field.getAnnotation(TreeNodeType.class) != null) { field.setAccessible(true); type = field.getInt(t); } if (field.getAnnotation(TreeNodeGId.class) != null) { field.setAccessible(true); gId = field.getInt(t); } if (field.getAnnotation(TreeNodeName.class) != null) { field.setAccessible(true); name = (String) field.get(t); } if (field.getAnnotation(TreeNodeValue.class) != null) { field.setAccessible(true); value = (String) field.get(t); } } node = new Node(id, pId, type, gId, name, value); nodes.add(node); } for (int i = 0; i < nodes.size(); i++) { Node n = nodes.get(i); for (int j = i + 1; j < nodes.size(); j++) { Node m = nodes.get(j); if (m.getpId() == n.getId()) { n.getChild().add(m); m.setParent(n); } else if (m.getId() == n.getpId()) { m.getChild().add(n); n.setParent(m); } } } //System.out.println(JSON.toJSONString(nodes,SerializerFeature.DisableCircularReferenceDetect) ); return nodes; } public static <T> List<Node> getSortedNodes(List<T> datas) throws IllegalArgumentException, IllegalAccessException { List<Node> nodes = convertDatas2Nodes(datas); return getRootNodes(nodes); } private static List<Node> getRootNodes(List<Node> nodes) { List<Node> root = new ArrayList<Node>(); for (Node node : nodes) { if (node.isRoot()) { root.add(node); } } return root; } /* [ { id:1, pId:0,gid:null,type:"1", name:"config_latest_version", open:true,value:"1.0.43"}, { id:2, pId:0,gid:null,type:"2", name:"app_info", open:true}, { id:3, pId:2,gid:null,type:"1", name:"app_latest_version",value:"3.1.44"}, { id:4, pId:2,gid:null,type:"1", name:"app_version_illustration",value:"www.baoyou.com android版本3.1.44"}, { id:5, pId:0,gid:null,type:"3", name:"country_code"}, { id:6, pId:5,gid:1,type:"1", name:"code",value:"1"}, { id:7, pId:5,gid:1,type:"1", name:"value",value:"+86"}, { id:8, pId:5,gid:1,type:"1", name:"sequence",value:"1"} { id:9, pId:0,gid:0,type:"3", name:"slide_show_list"}, { id:10, pId:9,gid:1,type:"1", name:"type",value:"slide_show_001"}, { id:11, pId:9,gid:1,type:"1", name:"effective_start_time",value:"2016-09-06 08:00:00"}, { id:12, pId:9,gid:1,type:"1", name:"effective_end_time",value:"2016-12-06 08:00:00"}, { id:13, pId:9,gid:1,type:"1", name:"times",value:"3"}, { id:14, pId:9,gid:1,type:"1", name:"sequence",value:"1"}, { id:15, pId:9,gid:1,type:"3", name:"picture_list"}, { id:16, pId:15,gid:1,type:"3", name:"code",value:"1"}, { id:17, pId:15,gid:1,type:"3", name:"vale",value:"https://www.baoyou.com/function_introduction/1.png?version=2016930"}, { id:18, pId:15,gid:1,type:"3", name:"url",value:""}, { id:19, pId:15,gid:1,type:"3", name:"sequence",value:"1"}, { id:20, pId:15,gid:2,type:"3", name:"code",value:"2"}, { id:21, pId:15,gid:2,type:"3", name:"vale",value:"https://www.baoyou.com/function_introduction/2.png?version=2016930"}, { id:22, pId:15,gid:2,type:"3", name:"url",value:""}, { id:23, pId:15,gid:2,type:"3", name:"sequence",value:"2"}, { id:24, pId:15,gid:3,type:"3", name:"code",value:"3"}, { id:25, pId:15,gid:3,type:"3", name:"vale",value:"https://www.baoyou.com/function_introduction/3.png?version=20161129"}, { id:26, pId:15,gid:3,type:"3", name:"url",value:""}, { id:27, pId:15,gid:3,type:"3", name:"sequence",value:"3"} { id:28, pId:9,gid:2,type:"3", name:"type",value:"slide_show_002"}, { id:29, pId:9,gid:2,type:"3", name:"effective_start_time",value:"2016-09-06 08:00:00"}, { id:30, pId:9,gid:2,type:"3", name:"effective_end_time",value:"2016-12-06 08:00:00"}, { id:31, pId:9,gid:2,type:"3", name:"times",value:"3"}, { id:32, pId:9,gid:2,type:"3", name:"sequence",value:"1"}, { id:33, pId:9,gid:2,type:"3", name:"picture_list"}, { id:34, pId:33,gid:1,type:"3", name:"code",value:"4"}, { id:35, pId:33,gid:1,type:"3", name:"vale",value:"https://www.baoyou.com/function_introduction/carouselfigure02.png?version=2016930"}, { id:36, pId:33,gid:1,type:"3", name:"url",value:""}, { id:37, pId:33,gid:1,type:"3", name:"sequence",value:"4"}, { id:38, pId:33,gid:2,type:"3", name:"code",value:"5"}, { id:39, pId:33,gid:2,type:"3", name:"vale",value:"https://www.baoyou.com/function_introduction/carouselfigure02.png?version=2016930"}, { id:40, pId:33,gid:2,type:"3", name:"url",value:""}, { id:41, pId:33,gid:2,type:"3", name:"sequence",value:"5"} ] */ @SuppressWarnings({ "unused", "rawtypes" }) public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException { Item item1 = new Item(1,0,1,0,"config_latest_version","1.0.43"); Item item2 = new Item(2,0,2,0,"app_info",null); Item item3 = new Item(3,2,1,0,"app_latest_version","3.1.44"); Item item4 = new Item(4,2,1,0,"app_version_illustration","www.baoyou.com android版本3.1.44"); Item item5 = new Item(5,0,3,0,"country_code",null); Item item6 = new Item(6,5,1,1,"code","1"); Item item7 = new Item(7,5,1,1,"value","+86"); Item item8 = new Item(8,5,1,1,"sequence","1"); Item item9 = new Item(9,0,3,0,"slide_show_list",null); Item item10 = new Item(10,9,1,1,"type","slide_show_001"); Item item11 = new Item(11,9,1,1,"effective_start_time","2016-09-06 08:00:00"); Item item12 = new Item(12,9,1,1,"effective_end_time","2016-09-06 08:00:00"); Item item13 = new Item(13,9,1,1,"times","3"); Item item14 = new Item(14,9,1,1,"sequence","1"); Item item15 = new Item(15,9,3,1,"picture_list",null); Item item16 = new Item(16,15,1,1,"code","1"); Item item17 = new Item(17,15,1,1,"vale","https://www.baoyou.com/function_introduction/1.png?version=2016930"); Item item18 = new Item(18,15,1,1,"url",""); Item item19 = new Item(19,15,1,1,"sequence","1"); Item item20 = new Item(20,15,1,2,"code","2"); Item item21 = new Item(21,15,1,2,"vale","https://www.baoyou.com/function_introduction/2.png?version=2016930"); Item item22 = new Item(22,15,1,2,"url",""); Item item23 = new Item(23,15,1,2,"sequence","2"); Item item24 = new Item(24,15,1,3,"code","3"); Item item25 = new Item(25,15,1,3,"vale","https://www.baoyou.com/function_introduction/3.png?version=20161129"); Item item26 = new Item(26,15,1,3,"url",""); Item item27 = new Item(27,15,1,3,"sequence","3"); Item item28 = new Item(28,9,1,2,"type","slide_show_002"); Item item29 = new Item(29,9,1,2,"effective_start_time","2016-09-06 08:00:00"); Item item30 = new Item(30,9,1,2,"effective_end_time","2016-09-06 08:00:00"); Item item31 = new Item(31,9,1,2,"times","3"); Item item32 = new Item(32,9,1,2,"sequence","2"); Item item33 = new Item(33,9,3,2,"picture_list",null); Item item34 = new Item(34,33,1,1,"code","1"); Item item35 = new Item(35,33,1,1,"vale","https://www.baoyou.com/function_introduction/1.png?version=2016930"); Item item36 = new Item(36,33,1,1,"url",""); Item item37 = new Item(37,33,1,1,"sequence","1"); Item item38 = new Item(38,33,1,2,"code","2"); Item item39 = new Item(39,33,1,2,"vale","https://www.baoyou.com/function_introduction/2.png?version=2016930"); Item item40 = new Item(40,33,1,2,"url",""); Item item41 = new Item(41,33,1,2,"sequence","2"); List <Item> list = Lists.newArrayList(item1,item2,item3,item4,item5,item6,item7,item8,item9,item10 ,item11,item12,item13,item14,item15,item16,item17,item18,item19,item20 ,item21,item22,item23,item24,item25,item26,item27 ,item28,item29,item30 ,item31,item32,item33,item34,item35,item36,item37,item38,item39,item40 ,item41 ); List<Node> nodes = convertDatas2Nodes(list); List<Node> sortedNodes = getSortedNodes(nodes); System.out.println(JSON.toJSONString(sortedNodes) ); Map mapresult = new TreeMap<String,String>(); Map result = JsonHelper.getResult(sortedNodes); System.out.println(JSON.toJSONString(result)); } }
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import www.baoyou.com.entity.Node; public class JsonHelper { @SuppressWarnings({ "rawtypes" }) public static Map getResult2(List<Node> list){ return getResult(list); } @SuppressWarnings({ "rawtypes", "unchecked" }) public static List getResult3(List<Node> list){ List root = new ArrayList<Map>(); Map<String,List> ll = new HashMap<String,List>(); for (Node n :list) { List llist = ll.get(n.pId +"@"+n.gId); if(llist == null){ llist = new ArrayList<Node>(); } llist.add(n); ll.put(n.pId +"@"+n.gId,llist); } for (List<Node> lnode : ll.values()) { root.add(getResult(lnode)); } return root; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map getResult(List<Node> list){ Map map = new TreeMap(); for (Node n : list) { switch (n.type) { case 1: map.put(n.name, n.value); break; case 2: map.put(n.name,getResult2(n.child)); break; case 3: map.put(n.name,getResult3(n.child)); break; default: break; } } return map; } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!