Json解析
public static void main(String[] args) { String json = "{\"code\":\"200\",\"msg\":\"数据获取成功\",\"data\":{\"codes\":900,\"HuKouBen\":\"zhangsan\",\"FangChanZheng1\":null}}"; /** * 将Json字符串转换为JsonObject */ JSONObject jsonObject = JSONObject.parseObject(json); getJson(jsonObject); List<String> list = new ArrayList<String>(); getJsonIsNullCount(jsonObject,0,list); } /** * 解析Json字符串 * @param jsonObj */ public static void getJson(JSONObject jsonObj) { String codeValue = jsonObj.getString("code"); if ("200".equals(codeValue)) { Object jsonArray = jsonObj.get("data"); JSONObject jsons = (JSONObject) JSONObject.toJSON(jsonArray); Object value = jsons.get("HuKouBen"); System.out.println(value); } } /** * 递归解析json字符串 * "{\"code\":\"200\",\"msg\":\"数据获取成功\",\"data\":{\"codes\":900,\"HuKouBen\":\"zhangsan\",\"FangChanZheng1\":null}}"; * @param jsonObj * @param count * @return */ public static int getJsonIsNullCount(JSONObject jsonObj, int count, List<String> list) { int coun = count; Iterator<Entry<String, Object>> it = jsonObj.entrySet().iterator(); while (it.hasNext()){ Entry<String, Object> obj = it.next(); String key = obj.getKey(); if ("data".equalsIgnoreCase(key)){ Object jsonArray = jsonObj.get("data"); //将会获取的Json串转化为JSONObject JSONObject jsons = (JSONObject) JSONObject.toJSON(jsonArray); coun = getJsonIsNullCount(jsons,coun,list); } if ("HuKouBen1".equalsIgnoreCase(key) || "FangChanZheng1".equalsIgnoreCase(key) || "ShiJiKongZhiRenZhengXinBaoGao".equalsIgnoreCase(key) || "PeiOuZhengXinBaoGao".equalsIgnoreCase(key) || "QiYeZhengXinBaoGao".equalsIgnoreCase(key) || "ShiErGeYueXiaoShouQingDan".equalsIgnoreCase(key) || "ShiErGeYueXiaoShouLiuShui1".equalsIgnoreCase(key) || "ShiErGeYueXiaoShouLiuShui2".equalsIgnoreCase(key)) { coun = getValeIsNullOrNot(obj, coun, list); } } return coun; } /** * 判斷值是否為null * @param obj * @param count * @return */ public static int getValeIsNullOrNot(Entry<String, Object> obj, int count, List<String> list) { // System.out.println(obj.getKey() + "," + obj.getValue()); if (obj.getValue() == null) { System.out.println(obj.getKey() + " value is null"); list.add(obj.getKey()); count++; } return count; }