加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【java 菜鸟自动化实践之三】net.sf.json json递归 删除key和key

发布时间:2020-12-16 19:47:10 所属栏目:百科 来源:网络整理
导读: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 J
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");  
	}
}

??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读