JSONObject rst = {"AIS-RST":"AIS-00000001","AIS-STATUS":"AIS-000000","RECORD":[{"0|5a4ed8721b71eeb4011b78184c6708bd":{"ANUM":"74","ATOTAL":"6","ISCHECKNUM":"69","ISNUM":"0","NOCHECKNUM":"0","NONUM":"0"}},{"0|5a4ed8721b71eeb4011b78186a2108be":{"ANUM":"29303","ATOTAL":"464","ISCHECKNUM":"29113","ISNUM":"19","NOCHECKNUM":"19","NONUM":"0"}}]};
//解析rst,然后得到一个数组,再通过JSONArray数组解析出来
JSONArray jsonDwa = (JSONArray) JSONArray.parseObject(rst).get("RECORD"); //建造一个map用于后面将json对象解析出来后的key和value进行储存 Map<String,String> mapMe = new HashMap<String, String>(); Set set; int length = jsonDwa.size(); //将数组进行遍历获得其内的json对象 for (int i = 0; i <length ; i++) { //因为获得的是json对,所以用JSONObject来接收 JSONObject jsonObject2 = (JSONObject) jsonDwa.get(i); //打印发现是json对象里面含有json对象 System.out.println("YYYYYYYYYY"+jsonObject2); //用一个set将json对象进行迭代,然后将其key和value分别放进map,这样就获得了json的key和value set = jsonObject2.keySet(); for(Object key : set){ String dwaSet = (String) key; mapMe.put(dwaSet,jsonObject2.get(dwaSet).toString()); } } System.out.println("mapMe"+ JSON.toJSONString(mapMe)+mapMe.size()); }catch (Exception e){ e.printStackTrace(); }
时间: 2024-10-23 22:43:16