java 通过 list 实现 json 生成

简介:   java  实现 json 拼接   功能:通过 item 添加数据的方式 ,解析拼接成 json 字符串   待优化项: 使用 jsonobject.put(name,value) 而并非 map.

 

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/



 
 
 谢谢您的赞助,我会做的更好!

目录
相关文章
|
1月前
|
安全 Java 程序员
深入Java集合框架:解密List的Fail-Fast与Fail-Safe机制
本文介绍了 Java 中 List 的遍历和删除操作,重点讨论了快速失败(fail-fast)和安全失败(fail-safe)机制。通过普通 for 循环、迭代器和 foreach 循环的对比,详细解释了各种方法的优缺点及适用场景,特别是在多线程环境下的表现。最后推荐了适合高并发场景的 fail-safe 容器,如 CopyOnWriteArrayList 和 ConcurrentHashMap。
58 5
|
29天前
|
Java 程序员 编译器
Java|如何正确地在遍历 List 时删除元素
从源码分析如何正确地在遍历 List 时删除元素。为什么有的写法会导致异常,而另一些不会。
24 3
|
29天前
|
Java 程序员
Java|List.subList 踩坑小记
不应该仅凭印象和猜测,就开始使用一个方法,至少花一分钟认真读完它的官方注释文档。
23 1
|
1月前
|
JSON JavaScript Java
在Java中处理JSON数据:Jackson与Gson库比较
本文介绍了JSON数据交换格式及其在Java中的应用,重点探讨了两个强大的JSON处理库——Jackson和Gson。文章详细讲解了Jackson库的核心功能,包括数据绑定、流式API和树模型,并通过示例演示了如何使用Jackson进行JSON解析和生成。最后,作者分享了一些实用的代码片段和使用技巧,帮助读者更好地理解和应用这些工具。
在Java中处理JSON数据:Jackson与Gson库比较
|
1月前
|
JSON 前端开发 JavaScript
json字符串如何转为list对象?
json字符串如何转为list对象?
257 7
|
1月前
|
JSON Java 数据格式
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
java操作http请求针对不同提交方式(application/json和application/x-www-form-urlencoded)
76 1
|
2月前
|
JSON Java fastjson
java小工具util系列3:JSON和实体类转换工具
java小工具util系列3:JSON和实体类转换工具
27 2
|
2月前
|
JSON 前端开发 JavaScript
java中post请求调用下载文件接口浏览器未弹窗而是返回一堆json,为啥
客户端调接口需要返回另存为弹窗,下载文件,但是遇到的问题是接口调用成功且不报错,浏览器F12查看居然返回一堆json,而没有另存为弹窗; > 正确的效果应该是:接口调用成功且浏览器F12不返回任何json,而是弹窗另存为窗口,直接保存文件即可。
143 2
|
3月前
|
Java
用JAVA架建List集合为树形结构的代码方法
这段代码定义了一个表示树形结构的 `Node` 类和一个用于构建树形结构的 `TreeController`。`Node` 类包含基本属性如 `id`、`pid`、`name` 和 `type`,以及子节点列表 `children`。`TreeController` 包含初始化节点列表并将其转换为树形结构的方法。通过过滤和分组操作实现树形结构的构建。详情可见:[代码示例链接1](http://www.zidongmutanji.com/zsjx/43551.html),[代码效果参考链接2](https://www.257342.com/sitemap/post.html)。
44 5
|
2月前
|
Java API 开发者
代码小妙招:用Java轻松获取List交集数据
在Java中获取两个 `List`的交集可以通过 `retainAll`方法和Java 8引入的流操作来实现。使用 `retainAll`方法更为直接,但会修改原始 `List`的内容。而使用流则提供了不修改原始 `List`、更为灵活的处理方式。开发者可以根据具体的需求和场景,选择最适合的方法来实现。了解和掌握这些方法,能够帮助开发者在实际开发中更高效地处理集合相关的问题。
98 1
下一篇
无影云桌面