import net.sf.json.*; import java.util.Iterator; public class Commons { static String keyss; static Object objectss; @SuppressWarnings("rawtypes") public Object skiptojson(Object expect_json,String skip_values){ if(expect_json instanceof JSONArray){ JSONArray objArray = (JSONArray)expect_json; for (int i = 0; i < objArray.size(); i++) { skiptojson(objArray.get(i),skip_values); } } //如果为json对象 else if(expect_json instanceof JSONObject){ JSONObject jsonObjectss = (JSONObject) expect_json; Iterator itss = jsonObjectss.keys(); while (itss.hasNext()) { keyss = itss.next().toString(); objectss = jsonObjectss.get(keyss); // 如果得到的是数组 if (objectss instanceof JSONArray) { if (keyss.equals(skip_values)) { //删除指定的key和key值 jsonObjectss.discard(skip_values); break; } else{ JSONArray objArray = (JSONArray) objectss; skiptojson(objArray,skip_values); } // 如果key中是一个json对象 else if (objectss instanceof JSONObject) { if (keyss.equals(skip_values)) { //删除指定的key和key值 jsonObjectss.discard(skip_values); break; } else { skiptojson((JSONObject) objectss,skip_values);} } // 如果key中是其他 else { if (keyss.equals(skip_values)) { //删除key和key值 jsonObjectss.discard(skip_values); break; } } } } System.out.println("002key:" + expect_json ); return expect_json; } public static void main(String[] args) throws Exception { //Checking_response_data 表示json字符串 SONObject expect_json=JSONObject.fromObject(Checking_response_data); System.out.println(">>>>>response_json:"+expect_json); skiptojson(expect_json,"total"); } }