由于我们是于c++做对接的,c++返回给我方的是一个无限极的部门结构的json数据体,现在要解释这个json结构体.由于是无限级,自然的就想到了用递归来实现.在此为记录.
接收的数据格式为:
{"code":"S_OK","result":[0,0,0,0,0],"var":
{
"deptList": [
{
"id":3,
"name":"邮件系统开发部2"
"deptList": [
{
"id":5,
"name":"企业邮箱"
"deptList": [
{
"id":6,
"name":"企业邮箱1",
"deptList": [
]
},
{
"id":7,
"name":"企业邮箱2"
"deptList": [
{
"id":8,
"name":"企业邮箱3"
"deptList": [
]
}
],
"userList": [
{
"id":24,
"uin":1239
}
]
}
]
}
]
},
{
"id":4,
"name":"平台开发部(pingtai)2"
"deptList": [
]
},
{
"id":9,
"name":"dept_name_app_88"
"deptList": [
]
}
]
}}
-在java 方构造pojo
import java.util.List;
public class Dept {
private String id;
private String name;
private List<Dept> deptList;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Dept> getDeptList() {
return deptList;
}
public void setDeptList(List<Dept> deptList) {
this.deptList = deptList;
}
}
import java.util.List;
public class Var {
private List<Dept> deptList;
public List<Dept> getDeptList() {
return deptList;
}
public void setDeptList(List<Dept> deptList) {
this.deptList = deptList;
}
}
public class Res {
private Var var;
public Var getVar() {
return var;
}
public void setVar(Var var) {
this.var = var;
}
}
些上类是为了将json转为对javabean 对像
测试类:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sbf=new StringBuffer();
sbf.append("{");
sbf.append("\"var\":");
sbf.append("{");
sbf.append("\"deptList\":");
sbf.append("[");
sbf.append("{");
sbf.append("\"id\":\"3\"").append(",");
sbf.append("\"name\":\"邮件系统开发部2\"").append(",");
sbf.append("\"deptList\":");
sbf.append("[");
sbf.append("{");
sbf.append("\"id\":\"5\"").append(",");
sbf.append("\"name\":\"企业邮箱\"").append(",");
sbf.append("\"deptList\":");
sbf.append("[");
sbf.append("{");
sbf.append("\"id\":\"6\"").append(",");
sbf.append("\"name\":\"企业邮箱1\"");
sbf.append("},");
sbf.append("{");
sbf.append("\"id\":\"7\"").append(",");
sbf.append("\"name\":\"企业邮箱2\"").append(",");
sbf.append("\"deptList\":");
sbf.append("[");
sbf.append("{");
sbf.append("\"id\":\"8\"").append(",");
sbf.append("\"name\":\"企业邮箱3\"").append(",");
sbf.append("\"deptList\":");
sbf.append("[");
sbf.append("{");
sbf.append("\"id\":\"9\"").append(",");
sbf.append("\"name\":\"企业邮箱4\"");
sbf.append("}");
sbf.append("]");
sbf.append("}");
sbf.append("]");
sbf.append("}");
sbf.append("]");
sbf.append("}");
sbf.append("]");
sbf.append("}").append(",");
sbf.append("{");
sbf.append("\"id\":\"4\"").append(",");
sbf.append("\"name\":\"平台开发部(pingtai)2\"");
sbf.append("}").append(",");
sbf.append("{");
sbf.append("\"id\":\"5\"").append(",");
sbf.append("\"name\":\"测试部\"").append(",");
sbf.append("\"deptList\":");
sbf.append("[");
sbf.append("{");
sbf.append("\"id\":\"6\"").append(",");
sbf.append("\"name\":\"测试部1\"");
sbf.append("}");
sbf.append("]");
sbf.append("}");
sbf.append("]");
sbf.append("}");
sbf.append("}");
System.out.println(sbf.toString());
Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("deptList", Dept.class);
classMap.put("dept", Dept.class);
Res resJson = (Res) JSONObject.toBean(getJsonObj(sbf.toString()), Res.class,classMap);
System.out.println(resJson.getVar().getDeptList());
List<Dept> list=resJson.getVar().getDeptList();
List<Dept> temp=new ArrayList<Dept>();
for(int i=0;i<list.size();i++)
{
Dept d=new Dept();
d.setId(list.get(i).getId());
d.setName(list.get(i).getName());
temp.add(d);
List<Dept> temp2=getdept(list.get(i));
if(temp2!=null)
{
for(int m=0;m<temp2.size();m++)
{
temp.add(temp2.get(m));
}
}
}
for(int k=0;k<temp.size();k++)
{
System.out.println("******id:"+temp.get(k).getId()+"******name:"+temp.get(k).getName());
}
}
public static JSONObject getJsonObj(String jsonStr)
{
return JSONObject.fromObject(jsonStr);
}
public static List<Dept> getdept(Dept dept)
{
List<Dept> temp=new ArrayList<Dept>();
List<Dept> list= dept.getDeptList();
if(list!=null)
{
for(int i=0;i<list.size();i++)
{
Dept d=new Dept();
d.setId(list.get(i).getId());
d.setName(list.get(i).getName());
temp.add(d);
if(list.get(i).getDeptList()!=null)
{
if(list.get(i).getDeptList().size()>0)
{
List<Dept> kk= getdept(list.get(i));
if(kk!=null)
{
for(int m=0;m<kk.size();m++)
{
temp.add(kk.get(m));
}
}
}
}
}
}
return temp;
}
}