首页 > 编程 > Java > 正文

java生成三级json对象

2019-11-08 03:07:28
字体:
来源:转载
供稿:网友

  在网页中我们经常会用到三级联动下拉菜单,而从后台获得的数据一般是json格式的,故需要返回三级json对象,另外有一些特殊情况也需要用到三级的json来实现。

1、文件Node1.java,定义Node1对象:

package com.soecode.hbdw.powstation.node;import java.util.List;public class Node1 {	PRivate String name;	private List<Node2> cName;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public List<Node2> getcName() {		return cName;	}	public void setcName(List<Node2> cName) {		this.cName = cName;	}	}

2、文件Node2.java,定义Node2对象:

package com.soecode.hbdw.powstation.node;import java.util.List;public class Node2 {	private String name;	private List<String> cName;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public List<String> getcName() {		return cName;	}	public void setcName(List<String> cName) {		this.cName = cName;	}	}

3、转换代码:

//显示所有	@ResponseBody	@RequestMapping(value = "/queryAll")	private List<Node1> getlist(Model model) {		List<PowStation> powStations=powStationService.getList();		int i,j,k;		//flag1和flag2用以标志对象存在与否,存在为false,否则为true		Boolean flag1,flag2;		List<Node1> nodes=new ArrayList<Node1>();		for(i=0;i<powStations.size();i++){			PowStation powstation=powStations.get(i);			String string1=powstation.getSubunit();			String string2=powstation.getName();			String string3=powstation.getNum();			flag1=true;			flag2=true;			if (string1!=null) {				Node1 node1=new Node1();				Node2 node2=new Node2();				List<String> list1=new ArrayList<String>();				List<Node2> list2=new ArrayList<Node2>();				for(j=0;j<nodes.size();j++){					if (string1.equals(nodes.get(j).getName())) {						flag1=false;						//break;						for(k=0;k<nodes.get(j).getcName().size();k++){							if (string2.equals(nodes.get(j).getcName().get(k).getName())) {								//设置node2结点,并添加node2结点到node1								node2.setName(string2);								list1=nodes.get(j).getcName().get(k).getcName();								list1.add(string3);								node2.setcName(list1);								//设置node1结点,并添加node2结点到nodes								node1.setName(string1);								list2=nodes.get(j).getcName();								list2.set(k, node2);								node1.setcName(list2);								nodes.set(j, node1);								flag2=false;								break;							}						}						if (flag2) {							//设置node2结点,并添加node2结点到node1							node2.setName(string2);							//list1=node2.getcName();							list1.add(string3);							node2.setcName(list1);							//设置node1结点,并添加node2结点到nodes							node1.setName(string1);							list2=nodes.get(j).getcName();							list2.add(node2);							node1.setcName(list2);								nodes.set(j, node1);													}					} 				}				if(flag1){					//设置node2结点,并添加node2结点到node1					node2.setName(string2);					list1.add(string3);					node2.setcName(list1);					//设置node1结点,并添加node2结点到nodes					node1.setName(string1);					list2.add(node2);					node1.setcName(list2);										nodes.add(node1);				}			}			}		return nodes;	}

  java对象实际以树的结构存储,以多级列表List和自定义javaBean相结合来实现树的存储,并通过@Responsebody注解来实现将返回值自动转化为三级json的对象。

  其中@RequestMapping(value = "/queryAll")是用来实现页面地址映射的,在这里可以不用管它。

  powStations是一个List列表对象,PowStation是一个pojo,包括id和表单的三级元素,第一级subunit,第二级name,第三级num。

  另外,需要注意第一个if判断,string1获取的对象是通过PowStation从数据库中获取的,当对象为null时,不能用equals()和matchs()判断,因为string1对象相当于未初始化,没有目标。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表