这是用org.json解析json的一个测试类,需要引入对应的两个包
import org.json.JSONArray;
import org.json.JSONObject;
例子中的json字符串有点长,是一个数组原型
try {
StringBuffer jsonString = new StringBuffer();
jsonString.append("[{'subdepts': [{'subdepts': [], 'name': '总经理', 'members': [{'loginname': 'songguangbin', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '宋广彬', 'title': '电子商务营销事业部总经理', 'mobile': ['0'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '运营总监', 'members': [{'loginname': 'liubo', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '刘波', 'title': '电子商务营销事业部运营总监', 'mobile': ['1'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '非淘平台部', 'members': [{'loginname': 'liuchen', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '刘辰', 'title': '非淘平台部经理', 'mobile': ['2'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'xingchengjie', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '邢承杰', 'title': '非淘平台运营专员', 'mobile': ['3'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '天猫平台部', 'members': [{'loginname': 'linxin', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '林辛', 'title': '天猫平台部经理', 'mobile': ['4'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'zhanglinan', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '张力楠', 'title': '运营专员', 'mobile': ['5'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'chenliliang', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '陈立亮', 'title': '广告推广专员', 'mobile': ['6'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '营销部', 'members': [{'loginname': 'zangyanyan', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '臧妍妍', 'title': '国际贸易客户经理', 'mobile': ['7'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'lingqianglei', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '凌强磊', 'title': '活动策划专员', 'mobile': ['8'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'houzheng', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '侯征', 'title': 'CRM专员', 'mobile': ['9'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '客服部', 'members': [{'loginname': 'songtingting', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '宋婷婷', 'title': '客服部经理', 'mobile': ['10'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'lizhaoxia', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '李召霞', 'title': '售前客服', 'mobile': ['11'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'yangbin', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '杨彬', 'title': '售前客服', 'mobile': ['12'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'maliman', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '马力曼', 'title': '售前客服', 'mobile': ['13'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'lvyongbin', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '吕永斌', 'title': '售前客服', 'mobile': ['14'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'liyujie', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '李玉洁', 'title': '售后客服', 'mobile': ['15'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '仓储物流部', 'members': [{'loginname': 'yuanpengyu', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '袁鹏宇', 'title': '仓储物流部经理', 'mobile': ['16'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'niuqinglong', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '牛庆龙', 'title': '仓储专员', 'mobile': ['17'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'zhangyukuan', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '张雨宽', 'title': '仓储专员', 'mobile': ['18'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'fuguiwei', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '付贵苇', 'title': '仓储专员', 'mobile': ['19'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '运维数据部', 'members': [{'loginname': 'wangbing', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '王兵', 'title': '运维数据部经理', 'mobile': ['20'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'niujunjie', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '牛俊杰', 'title': '软件开发工程师', 'mobile': ['21'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'lilou', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '李楼', 'title': '软件开发工程师', 'mobile': ['22'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'sunyan', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '孙磊', 'title': '运维专员', 'mobile': ['23'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '设计部', 'members': [{'loginname': 'zhulin', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '朱琳', 'title': '设计部经理', 'mobile': ['24'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'songruijin', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '宋瑞进', 'title': '网页设计师', 'mobile': ['25'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '策划推广部', 'members': [{'loginname': 'zhaozhexin', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '赵哲鑫', 'title': '总监', 'mobile': ['26'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'songkefeng', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '宋科锋', 'title': '部门副总监', 'mobile': ['27'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'chenxue', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '陈雪', 'title': 'SNS运营专员', 'mobile': ['28'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'wangwei', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '王巍', 'title': '策划文案', 'mobile': ['29'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'zhaofang', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '赵芳', 'title': '媒介专员', 'mobile': ['30'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'qiqi', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '戚琦', 'title': '网络推广专员', 'mobile': ['31'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'xinglong', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '邢隆', 'title': '活动主管', 'mobile': ['32'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'yintao', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '尹涛', 'title': '活动主管', 'mobile': ['33'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'wangchun', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '王纯', 'title': '市场推广专员(实习生)', 'mobile': ['34'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'cuixiuwen', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '崔秀文', 'title': '市场推广专员', 'mobile': ['35'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'youxue', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '游雪', 'title': '市场推广专员', 'mobile': ['36'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'liyang', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '李洋', 'title': '市场推广专员', 'mobile': ['37'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'zhangbopeng', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '张鹏博', 'title': '市场推广专员', 'mobile': ['38'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'tongding', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '佟鼎', 'title': '市场推广专员', 'mobile': ['39'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'wangxuejian', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '王雪健', 'title': '市场推广专员', 'mobile': ['40'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '商超部', 'members': [{'loginname': 'xiewei', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '解伟', 'title': '商超部副总监', 'mobile': ['41'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'zhaoxurong', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '赵绪荣', 'title': '商场运营经理', 'mobile': ['42'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'dingjinying', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '丁进英', 'title': '推广专员', 'mobile': ['43'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'wanglijuan', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '王立娟', 'title': '推广专员', 'mobile': ['44'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'wangxue', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '王雪', 'title': '店员', 'mobile': ['45'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'xunannan', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '徐南南', 'title': '店员', 'mobile': ['46'], 'msn': '', 'email': ['[email protected]']}, {'loginname': 'liuxiaojie', 'sex': '女', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '刘晓洁', 'title': '店员', 'mobile': ['47'], 'msn': '', 'email': ['[email protected]']}]}, {'subdepts': [], 'name': '大客户部', 'members': [{'loginname': 'zhaobin', 'sex': '男', 'phone': ['2357.0'], 'wechat': '', 'password': '123qwe', 'qq': 0, 'name': '赵滨', 'title': '大客户部总监', 'mobile': ['48'], 'msn': '', 'email': ['[email protected]']}]}], 'name': '全体人员', 'members': []}]");
JSONArray jsonArray= new JSONArray(jsonString.toString());
for(int i=0;i<jsonArray.length();i++){
//创建根节点
JSONObject jsons=(JSONObject) jsonArray.get(i);
JSONArray arraytemp = jsons.getJSONArray("subdepts");
System.out.println(arraytemp);
for (int k = 0; k < arraytemp.length(); k++) {
JSONObject obj =(JSONObject) arraytemp.get(k);
System.out.println("部门:"+obj.getString("name"));
JSONArray arr = obj.getJSONArray("members");
System.out.println("arr:"+arr.length());
for (int j = 0; j < arr.length(); j++) {//解析每个部门下所有人员信息
JSONObject job = arr.getJSONObject(j);
System.out.println("password:"+job.getString("password"));
System.out.println("sex:"+job.getString("sex"));
System.out.println("title:"+job.getString("title"));
System.out.println("loginname:"+job.getString("loginname"));
System.out.println("name:"+job.getString("name"));
}
}
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
生成json数据
/**
* 创建json数据
* @return
*/
public String biuldJson() {
JSONObject json=new JSONObject();
try {
JSONArray jsonMembers = new JSONArray();
//人员
JSONObject user = new JSONObject();
user.put("userName", "zhangsan");
user.put("password", "1234");
user.put("tel","12345678");
jsonMembers.put(user);
//部门
JSONObject dept = new JSONObject();
dept.put("deptName", "研发部");
dept.put("deptID", "000001");
jsonMembers.put(dept);
//生成json数组数据
json.put("users", jsonMembers);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return json.toString();
}