1. 程式人生 > >比較兩個JSON陣列是否相同並返回不同的JSON物件

比較兩個JSON陣列是否相同並返回不同的JSON物件

 比較兩個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;
	}
}