firemail

标题: android java 解析复杂json串 [打印本页]

作者: java    时间: 2017-4-5 18:27
标题: android java 解析复杂json串
  1. {"timeStamp":1490856881059,"emailClassificationDetailResultList":[{"emailClassificationBvo":{"id":3,"name":"购物促销"},"emailClassificationKeysBvoList":[{"id":1,"name":"双11","weight":1},{"id":2,"name":"订单","weight":2}]},{"emailClassificationBvo":{"id":4,"name":"银行金融"},"emailClassificationKeysBvoList":[{"id":3,"name":"信用卡","weight":1},{"id":4,"name":"银行","weight":2}]},{"emailClassificationBvo":{"id":5,"name":"其他信息"},"emailClassificationKeysBvoList":[]}]}
复制代码
  1. import org.json.JSONArray;
  2.         import org.json.JSONObject;


  3.         String classAllJson = "{\"timeStamp\":1490856881059,\"emailClassificationDetailResultList\":[{\"emailClassificationBvo\":{\"id\":3,\"name\":\"购物促销\"},\"emailClassificationKeysBvoList\":[{\"id\":1,\"name\":\"双11\",\"weight\":1},{\"id\":2,\"name\":\"订单\",\"weight\":2}]},{\"emailClassificationBvo\":{\"id\":4,\"name\":\"银行金融\"},\"emailClassificationKeysBvoList\":[{\"id\":3,\"name\":\"信用卡\",\"weight\":1},{\"id\":4,\"name\":\"银行\",\"weight\":2}]},{\"emailClassificationBvo\":{\"id\":5,\"name\":\"其他信息\"},\"emailClassificationKeysBvoList\":[]}]}";
  4.         //String classAllJson = "{\"timeStamp\":1490856881059}";
  5.         try {
  6.             JSONObject jsonObject = new JSONObject(classAllJson);
  7.             long timeStamp = jsonObject.getLong("timeStamp");
  8.             JSONArray classDetailArray = jsonObject.getJSONArray("emailClassificationDetailResultList");
  9.             for (int i = 0; i < classDetailArray.length(); i++){
  10.                 JSONObject classInfo = (JSONObject) classDetailArray.get(i);
  11.                 JSONObject classBaseInfo = classInfo.getJSONObject("emailClassificationBvo");
  12.                 long id = classBaseInfo.getLong("id");
  13.                 String name = classBaseInfo.getString("name");
  14.                 JSONArray classKeysArray = classInfo.getJSONArray("emailClassificationKeysBvoList");
  15.                 for (int j = 0; j < classKeysArray.length(); j++) {
  16.                     JSONObject keyInfo = (JSONObject) classKeysArray.get(i);
  17.                     long idKey = keyInfo.getLong("id");
  18.                     String nameKey = keyInfo.getString("name");
  19.                     Integer weight = keyInfo.getInt("weight");
  20.                     System.out.println(weight);
  21.                 }
  22.             }

  23.         } catch (Exception e) {
  24.             System.out.println("parseObject Exception " + e);
  25.         }
复制代码





欢迎光临 firemail (http://firemail.wang:8088/) Powered by Discuz! X3