`
knight_black_bob
  • 浏览: 857292 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java 通过 list<item> 实现 json 生成

阅读更多

 

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/



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

0
0
分享到:
评论

相关推荐

    json解析+异步加载+listView

    ArrayAdapter&lt;Item&gt; adapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); } } // 在适当的地方启动任务 new LoadDataTask().execute(jsonData); `...

    jQuery的Ajax实现异步传输List、Map

    本话题将深入探讨如何使用jQuery的Ajax功能实现异步传输List和Map类型的数据,同时涉及JSON数据格式的使用。 首先,让我们了解JSON(JavaScript Object Notation),它是轻量级的数据交换格式,易于人阅读和编写,...

    xml Document to Json

    List&lt;Item&gt; item; // getters and setters } class Item { String id; String name; // getters and setters } ``` 在这个例子中,我们定义了与XML结构相对应的Java类`RootObject`和`Item`,然后使用`Xml...

    Struts2 Json示例(Maven项目)

    items = new ArrayList&lt;&gt;(); items.add("Item 1"); items.add("Item 2"); items.add("Item 3"); return "success"; } } ``` 然后,在Struts配置文件中定义这个Action,指定返回的视图是JSON类型: ```xml ...

    Freemarker小demo

    &lt;#list data.array as item&gt; &lt;li&gt;${item.property}&lt;/li&gt; &lt;/#list&gt; &lt;/ul&gt; &lt;!-- 示例HTML截取 --&gt; &lt;div&gt;${truncateHtml(originalHtml, 100)}&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; ``` 这个模板展示了如何展示JSON数据中的...

    将集合转换为Json的相关jar包

    List&lt;String&gt; list = new ArrayList&lt;&gt;(); list.add("Item1"); list.add("Item2"); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(list); ``` 2. **Gson**: Google...

    AJAX和struts2传递JSON数组

    items = gson.fromJson(jsonString, new TypeToken&lt;List&lt;MyObject&gt;&gt;(){}.getType()); // ...其他业务逻辑... return SUCCESS; } } ``` 在这里,`MyObject`是你定义的一个Java类,其属性应与JSON对象的键相对应...

    struts2+json结合

    Struts2是一个非常流行的Java Web框架,它极大地简化了MVC(模型-视图-控制器)应用程序的...通过以上步骤,你可以在你的Java Web项目中实现Struts2与JSON的集成。记得根据实际情况调整代码示例,以满足你的具体需求。

    servlet+jquery表格数据填充

    List&lt;DataObject&gt; dataList = new ArrayList&lt;&gt;(); // 数据库连接、查询、获取结果集等操作 // ... // 将查询结果转换为List // ... // 设置响应内容类型为JSON response.setContentType("application/json");...

    jsp 选定要删除的

    List&lt;String&gt; deletedIds = new ArrayList&lt;&gt;(); for (String id : ids) { if (deleteItemFromDatabase(id)) { deletedIds.add(id); } } // 返回删除成功的项目ID列表 response.setContentType("application...

    List转JSON的jar包

    List&lt;String&gt; dataList = new ArrayList&lt;&gt;(); dataList.add("Item1"); dataList.add("Item2"); String jsonString = ListToJsonUtil.toJson(dataList); System.out.println(jsonString); // 输出:["Item1",...

    java转换xml、list、map和json(20210924214538)[借鉴].pdf

    以上就是Java中XML、List、Map与JSON之间转换的基本实现。需要注意的是,这些示例都是基于`json-lib`库,实际应用中可能还需要根据具体需求进行调整。在选择转换库时,除了`json-lib`,还有其他流行的库如Jackson、...

    struts2+jquery+json+List

    var row = '&lt;tr&gt;&lt;td&gt;' + item.property1 + '&lt;/td&gt;&lt;td&gt;' + item.property2 + '&lt;/td&gt;&lt;/tr&gt;'; $('table tbody').append(row); }); ``` 在压缩包`struts_jason`中,可能包含了示例代码、HTML模板、CSS样式以及...

    ListView for JSON.

    List&lt;Item&gt; items = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject itemObject = jsonArray.getJSONObject(i); String name = itemObject.getString("name"); String ...

    Json转化为Java对象的实例详解

    Map&lt;String, Class&gt; listMap = new HashMap&lt;&gt;(); listMap.put("books", Book[].class); InfoWithBooks infoWithBooks = (InfoWithBooks) JSONObject.toBean(jsonObject, InfoWithBooks.class, listMap); ``` 在这个...

    json实现jsp分页实例介绍(附效果图).docx

    &lt;li class="page-item"&gt;&lt;a class="page-link" href="#" data-page="3"&gt;3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/nav&gt; &lt;/body&gt; &lt;/html&gt; ``` #### 四、总结 本文介绍了如何使用JSON和JSP实现分页功能。通过设计JavaBean模型、编写...

    在JSP页面用JS处理从服务器返回的LIST集合

    2. JSP中的数据传递:在JSP页面中,我们可以使用`&lt;script&gt;`标签和EL(Expression Language)或者JSTL(JavaServer Pages Standard Tag Library)来将JSON字符串传递给JavaScript。例如: ```jsp &lt;script&gt; var ...

    highcharts加载数据库数据(java版)

    在Java端,我们将使用Java后端框架(如Spring MVC或Struts2)来处理数据,然后将数据转换成JSON格式,因为Highcharts能够直接解析JSON数据来生成图表。以下是一个基本流程: 1. **连接数据库**:使用Java的JDBC API...

Global site tag (gtag.js) - Google Analytics