比较两个JSON数组是否相同并返回不同的JSON对象

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LOVE_HopeOne/article/details/83749336

 比较两个Json数组是否相同

         比较两个数组,尤其是比较两个Jsonarray数组的不同,相关的解决方案真的很少,在我要写这个博客之前一直没有找到一个好的解决方法,直到发现  HashSet   这个类型的数组特性,在解决问题后我希望更多的人能够了解这一特性,并能解决这个比较数组元素不同的问题,问题没有大小之分,但是解决不了的问题就是大问题。

         下面代码中  我定义了两个JSONarray数组,每个JSONarray 数组中都装着几个JSON 对象,每个对象里面又有两个属性。比较这两个jsonarray数组  不同的json对象。然后 通过 加强for 循环输出不同的json对象。

package tools;

import java.util.HashSet;
import java.util.Set;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class testclass {

	public static void main(String[] args) {
		
     //定义第一个  json数组
		JSONArray  jsonArray= new JSONArray ();
			
//     以及数组里面七个对象,每个对象有两个属性。
		JSONObject jo=new JSONObject();
		JSONObject jo1=new JSONObject();
		JSONObject jo2=new JSONObject();
		JSONObject jo3=new JSONObject();
		JSONObject jo4=new JSONObject();
		JSONObject jo5=new JSONObject();
		JSONObject jo14=new JSONObject();
		
		jo.put("1", "xiaobao");
		jo.put("2", "2321");
		
		jo1.put("1", "asd");
		jo1.put("2", "as3d");
		
		jo2.put("1", "11");
		jo2.put("2", "11");
	
		
		jo3.put("1", "sd");
		jo3.put("2", "sd");
		
		jo4.put("1", "xiaozxcxbao");
		jo4.put("2", "xia544o");
		
		jo5.put("1", "zxc");
		jo5.put("2", "z7x7c");
		
		jo14.put("1", "xiaobao");
		jo14.put("2", "2321222222222");
	      
		jsonArray.add(jo); 
		jsonArray.add(jo1); 
		jsonArray.add(jo2); 
		jsonArray.add(jo3); 
		jsonArray.add(jo4); 
		jsonArray.add(jo5); 
		jsonArray.add(jo14); 

//       定义  第二个json数组,n个对象以及对象中封装的两个属性
		
		JSONArray  jsonArray1= new JSONArray ();
	
		JSONObject jo6=new JSONObject();
		JSONObject jo7=new JSONObject();
		JSONObject jo8=new JSONObject();
		JSONObject jo9=new JSONObject();
		JSONObject jo10=new JSONObject();
		JSONObject jo11=new JSONObject();
		

		jo6.put("1", "xiaobao");
		jo6.put("2", "232122222222222222");
		
		jo7.put("1", "diff12");
		jo7.put("2", "xia544o");
		
		jo8.put("1", "zxc");
		jo8.put("2", "z7x7c");

		jo9.put("1", "11");
		jo9.put("2", "11");
		
		jo10.put("1", "xiaozxcxbao");
		jo10.put("2", "xia34o");
		
		jo11.put("1", "zxc");
		jo11.put("2", "z7x7c");
		jsonArray1.add(jo6);
		jsonArray1.add(jo7);
		jsonArray1.add(jo8);
		jsonArray1.add(jo9);
		jsonArray1.add(jo10);
		jsonArray1.add(jo11);
		
		Set<JSONObject> it =getsame(jsonArray, jsonArray1);
	    
  //    输出返回的不同 json对象。   
		for (JSONObject j:it) {
			System.out.println( "   j  "+j);
			
		}
	
	       
	}
//    Set 数组特性,返回不同的对象
	public static Set<JSONObject> getsame(JSONArray a, JSONArray b) {

		Set<JSONObject> diff = new HashSet<JSONObject>(); // 用来存放两个数组中相同的元素
		Set<JSONObject> temp = new HashSet<JSONObject>(); // 用来存放数组a中的元素

		
		for (int i = 0; i < a.size(); i++) {
			JSONObject jo=new JSONObject();
	
			jo.put("1",a.getJSONObject(i).get("1")); // 把数组a中的元素放到Set中,可以去除重复的元素
			jo.put("2", a.getJSONObject(i).get("2"));
			temp.add(jo);
		}

		for (int j = 0; j < b.size(); j++) {
			// 把数组b中的元素添加到temp中
			JSONObject jo=new JSONObject();
			jo.put("1",b.getJSONObject(j).get("1")); // 把数组a中的元素放到Set中,可以去除重复的元素
			jo.put("2", b.getJSONObject(j).get("2"));
			// 如果temp中已存在相同的元素,则temp.add(b[j])返回false
			if (temp.add(jo)) {
				diff.add(jo);

			}
		}
		    return diff;
	}
}

猜你喜欢

转载自blog.csdn.net/LOVE_HopeOne/article/details/83749336