[{"code":"I1_5200","name":"移動繳費","type":"4","categories":[{"code":"I1_5200_014201","name":"湖北移動充值"}]},{"code":"I1_5200","name":"聯通繳費","type":"5","categories":[{"code":"J0_5200_0270","name":"湖北聯通代理商回繳款"},{"code":"I1_5200_024201","name":"湖北聯通充值"}]},{"code":"I1_5200","name":"電信繳費","type":"6","categories":[{"code":"I1_5200_034201","name":"湖北電信充值"}]},{"code":"D4_5200","name":"水費繳納","type":"1","categories":[{"code":"D4_5200_0000","name":"湖北省武漢市水費集團"},{"code":"D4_5200_0003","name":"蔡甸供水"}]},{"code":"D1_5200","name":"電費繳納","type":"2","categories":[{"code":"D1_5200_0000","name":"湖北省電力"},{"code":"D1_5200_5210","name":"湖北電費【武漢】"}]},{"code":"G1_5200","name":"ETC繳費","type":"39","categories":[{"code":"G1_5200_0000","name":"湖北ETC繳費(車牌顏色:藍色)"},{"code":"G1_5200_0001","name":"湖北ETC繳費(車牌顏色:黃色)"},{"code":"G1_5200_0002","name":"湖北ETC繳費(車牌顏色:黑色)"},{"code":"G1_5200_0003","name":"湖北ETC繳費(車牌顏色:白色)"}]},{"code":"S0_5200","name":"稅費","type":"20","categories":[{"code":"S0_5200_ACP01","name":"湖北地稅"}]},{"code":"G1_5200","name":"交通罰款","type":"26","categories":[{"code":"G1_5200_0004","name":"湖北交通罰款繳費"}]},{"code":"I1_5200","name":"固話寬帶","type":"44","categories":[{"code":"I1_5200_0600","name":"【電信固話】"}]},{"code":"S2_5200","name":"醫療充值","type":"47","categories":[{"code":"S2_5200_0000","name":"【健康之路賬戶充值】"}]}]
我要得到type=2的數據下面categories的子數據,過濾掉其他的!
方法很多,用ali 的fastjson解析的如下:
//json 字符串
String jsonStr = "[{\"code\":\"I1_5200\",\"name\":\"移動繳費\",\"type\":\"4\",\"categories\":[{\"code\":\"I1_5200_014201\",\"name\":\"湖北移動充值\"}]},{\"code\":\"I1_5200\",\"name\":\"聯通繳費\",\"type\":\"5\",\"categories\":[{\"code\":\"J0_5200_0270\",\"name\":\"湖北聯通代理商回繳款\"},{\"code\":\"I1_5200_024201\",\"name\":\"湖北聯通充值\"}]},{\"code\":\"I1_5200\",\"name\":\"電信繳費\",\"type\":\"6\",\"categories\":[{\"code\":\"I1_5200_034201\",\"name\":\"湖北電信充值\"}]},{\"code\":\"D4_5200\",\"name\":\"水費繳納\",\"type\":\"1\",\"categories\":[{\"code\":\"D4_5200_0000\",\"name\":\"湖北省武漢市水費集團\"},{\"code\":\"D4_5200_0003\",\"name\":\"蔡甸供水\"}]},{\"code\":\"D1_5200\",\"name\":\"電費繳納\",\"type\":\"2\",\"categories\":[{\"code\":\"D1_5200_0000\",\"name\":\"湖北省電力\"},{\"code\":\"D1_5200_5210\",\"name\":\"湖北電費【武漢】\"}]},{\"code\":\"G1_5200\",\"name\":\"ETC繳費\",\"type\":\"39\",\"categories\":[{\"code\":\"G1_5200_0000\",\"name\":\"湖北ETC繳費(車牌顏色:藍色)\"},{\"code\":\"G1_5200_0001\",\"name\":\"湖北ETC繳費(車牌顏色:黃色)\"},{\"code\":\"G1_5200_0002\",\"name\":\"湖北ETC繳費(車牌顏色:黑色)\"},{\"code\":\"G1_5200_0003\",\"name\":\"湖北ETC繳費(車牌顏色:白色)\"}]},{\"code\":\"S0_5200\",\"name\":\"稅費\",\"type\":\"20\",\"categories\":[{\"code\":\"S0_5200_ACP01\",\"name\":\"湖北地稅\"}]},{\"code\":\"G1_5200\",\"name\":\"交通罰款\",\"type\":\"26\",\"categories\":[{\"code\":\"G1_5200_0004\",\"name\":\"湖北交通罰款繳費\"}]},{\"code\":\"I1_5200\",\"name\":\"固話寬帶\",\"type\":\"44\",\"categories\":[{\"code\":\"I1_5200_0600\",\"name\":\"【電信固話】\"}]},{\"code\":\"S2_5200\",\"name\":\"醫療充值\",\"type\":\"47\",\"categories\":[{\"code\":\"S2_5200_0000\",\"name\":\"【健康之路賬戶充值】\"}]}]";
JSONArray array = JSON.parseArray(jsonStr);
for(int i=0;i<array.size();i++) {
JSONObject json = (JSONObject) array.get(i);
String type = json.getString("type");
String categories = json.getString("categories");
if("4".equals(type)) {
JSONArray cates = json.parseArray(categories);
for(int j=0;j<cates.size();j++) {
JSONObject cate = (JSONObject) cates.get(j);
System.out.println(cate.get("code"));
System.out.println(cate.get("name"));
}
}
}