Android json解析

来源:互联网 发布:java判断文件类型 编辑:程序博客网 时间:2024/04/29 21:52

这个例子很简单,

服务器返回结果:

{"status":1,"msg":"登陆成功!","role":"01","data":[{"EmployeeCode":"000743","EmployeeName":"000743","EmployeeTypeCode":"01"}]}


String jsonxml = {\"status\":1,\"msg\":\"登陆成功!\",\"role\":\"01\",\"data\":[{\"EmployeeCode\":\"000743\",\"EmployeeName\":\"000743\",\"EmployeeTypeCode\":\"01\"}]};
if(!"".equals(jsonxml)){
JSONObject jsonObject=new JSONObject(jsonxml);
user.setRole(jsonObject.getString("role"));
user.setStatus(jsonObject.getInt("status"));
user.setMsg(jsonObject.getString("msg"));
String data=jsonObject.getString("data");
//data是数组
JSONArray jsonArray=new JSONArray(data);
//数组下是对象
JSONObject object=new JSONObject(jsonArray.get(0).toString());
user.setEmployeeCode(object.getString("EmployeeCode"));
user.setEmployeeName(object.getString("EmployeeName"));
user.setEmployeeTypeCode(object.getString("EmployeeTypeCode"));
}


其实只要掌握了原理,再复杂的也搞得定,记住:遇到{}要转换成对象,遇到[]转换成数组,然后再获取就是了。不管多复杂的结构也可以给它解决。


最开始,接口给的有问题,然后我就按那个复杂的接口解析了返回的数据,一层一层的,一点也不能错啊,可是接口一改,哼哼,白写了,不过经验学到了,就那两句话,记住。这是我白写了的代码,舍不得。程序员最怕什么,最怕自己费了好大劲写出来的代码,结果接口一改,哈哈,改吧,他们决定我们。

if(!"".equals(jsonxml)){
// JSONObject jsonObject=new JSONObject(jsonxml);
// user.setRole(jsonObject.getString("role"));
// user.setStatus(jsonObject.getInt("status"));
// user.setMsg(jsonObject.getString("msg"));
// String data=jsonObject.getString("data");
// JSONArray array=new JSONArray(data);
// int count=array.length();
// if("03".equals(jsonObject.get("role"))){//解析的是学生登录的
// //数组里面是对象,所以要创建jsonobject
// JSONObject object=new JSONObject(array.get(0).toString());
// user.setEmployeeCode(object.getString("EmployeeCode"));
// user.setEmployeeName(object.getString("EmployeeName"));
// user.setEmployeeTypeCode(object.getString("EmployeeTypeCode"));
// }else if("01".equals(jsonObject.get("role"))){//教师
// //数组里面是数组,所以要创建jsonarray
// if(count==3){//部门负责人解析出来的有3项【数组里面有三个数组】
// //第一个数组下一个对象
// JSONArray arraychild1=new JSONArray(array.get(0).toString());
// JSONObject o1=new JSONObject(arraychild1.get(0).toString());
// user.setDEPARTID(o1.getString("DEPARTID"));
// user.setNAME(o1.getString("NAME"));
// //第二个数组下有两个对象
// JSONArray arraychild2=new JSONArray(array.get(1).toString());
// JSONObject o2=new JSONObject(arraychild2.get(0).toString());
// user.setAccountYear(o2.getString("AccountYear"));
// JSONObject o3=new JSONObject(arraychild2.get(1).toString());
// user.setAccountYear2(o3.getString("AccountYear"));
// //第三个数组下有一个对象
// JSONArray arraychild3=new JSONArray(array.get(2).toString());
// JSONObject o4=new JSONObject(arraychild3.get(0).toString());
// user.setEmployeeCode(o4.getString("EmployeeCode"));
// user.setEmployeeName(o4.getString("EmployeeName"));
// user.setEmployeeTypeCode(o4.getString("EmployeeTypeCode"));
// user.setIsChargeMan(o4.getString("IsChargeMan"));
// }else if(count==2){//普通教师数组里面有两个数组
// //第一个数组下有两个对象
// JSONArray arraychild1=new JSONArray(array.get(0).toString());
// JSONObject o1=new JSONObject(arraychild1.get(0).toString());
// user.setAccountYear(o1.getString("AccountYear"));
// JSONObject o2=new JSONObject(arraychild1.get(1).toString());
// user.setAccountYear2(o2.getString("AccountYear"));
// //第二个数组下有一个对象
// JSONArray arraychild2=new JSONArray(array.get(1).toString());
// JSONObject o3=new JSONObject(arraychild2.get(0).toString());
// user.setEmployeeCode(o3.getString("EmployeeCode"));
// user.setEmployeeName(o3.getString("EmployeeName"));
// user.setEmployeeTypeCode(o3.getString("EmployeeTypeCode"));
// user.setIsChargeMan(o3.getString("IsChargeMan"));
// }
// }
// }