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

这篇博客探讨了在缺乏直接解决方案的情况下,如何利用HashSet的特性来比较两个JSON数组的差异。作者创建了包含JSON对象的JSONArray,并通过增强for循环展示不同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;
	}
}

 

您可能感兴趣的与本文相关的镜像

AutoGPT

AutoGPT

AI应用

AutoGPT于2023年3月30日由游戏公司Significant Gravitas Ltd.的创始人Toran Bruce Richards发布,AutoGPT是一个AI agent(智能体),也是开源的应用程序,结合了GPT-4和GPT-3.5技术,给定自然语言的目标,它将尝试通过将其分解成子任务,并在自动循环中使用互联网和其他工具来实现这一目标

### 合两个JSON数组 在Java中,可以利用`org.json.JSONArray`类来处理JSON数据。为了合两个JSON数组,首先需要创建这两个JSONArray对象,然后遍历其中一个将它的元素添加到另一个中。 下面是一个具体的例子: ```java import org.json.JSONArray; public class JsonArrayMerger { public static JSONArray mergeJsonArrays(JSONArray jsonArray1, JSONArray jsonArray2) { // 创建一个新的JSONArray用于存储合后的结果 JSONArray resultArray = new JSONArray(); // 将第一个jsonArray中的所有元素加入resultArray for (int i = 0; i < jsonArray1.length(); i++) { resultArray.put(jsonArray1.get(i)); } // 将第二个jsonArray中的所有元素也加入resultArray for (int i = 0; i < jsonArray2.length(); i++) { resultArray.put(jsonArray2.get(i)); } return resultArray; } public static void main(String[] args) throws Exception { String jsonString1 = "[\"apple\", \"banana\"]"; String jsonString2 = "[\"orange\", \"grape\"]"; JSONArray jsonArray1 = new JSONArray(jsonString1); JSONArray jsonArray2 = new JSONArray(jsonString2); System.out.println(mergeJsonArrays(jsonArray1, jsonArray2)); } } ``` 这段代码展示了如何定义一个方法`mergeJsonArrays`接收两个参数作为输入——即要被合两个JSONArray返回它们合之后的新JSONArray实例。此外,在main函数里给出了调用此方法的具体案例[^1]。 需要注意的是上述示例假设所有的元素都是字符串形式;如果实际应用中有不同类型的元素,则可能需要额外考虑类型转换等问题。 #### 使用第三方库简化操作 除了手动编写循环外,还可以借助一些流行的JSON处理库如Gson或Jackson来更方便地完成这项工作。这些库提供了更加简洁的方式来进行复杂的JSON解析和序列化任务。 例如使用Jackson库实现相同的功能会更为简单直观: ```java import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; public class JacksonExample { private static final ObjectMapper objectMapper = new ObjectMapper(); public static ArrayNode combineJsonArrays(JsonNode firstArray, JsonNode secondArray){ ArrayNode combinedArray = objectMapper.createArrayNode(); ((ArrayNode)firstArray).forEach(combinedArray::add); ((ArrayNode)secondArray).forEach(combinedArray::add); return combinedArray; } public static void main(String[] args)throws Exception{ String jsonStr1="[1,\"two\",true]"; String jsonStr2="[{\"key\":\"value\"},null,[7,8]]"; JsonNode arrayOne = objectMapper.readTree(jsonStr1); JsonNode arrayTwo = objectMapper.readTree(jsonStr2); System.out.println(combineJsonArrays(arrayOne,arrayTwo)); } } ``` 这里通过引入ObjectMapper工具类以及其提供的APIs使得整个过程变得更加流畅易懂[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值