【Java】+【JSON】+对比两个json对象是否完全一样

一、需求

需要对比这2个json字符串是否完全一样(不用管顺序)

1     String dui = "{\"adGroupVO\":{\"campaignId\":\"CAMPAIGN201912101000004559\",\"adGroupChannel\":{\"channelType\":\"SMS\",\"resourceCode\":\"TEMPLATE_CODE\"},\"deliveryScene\":\"default\",\"adGroupTimeConfigVO\":{\"startDate\":\"2020-01-02\",\"endDate\":\"2020-01-04\",\"adGroupTimeConfigDetailVoList\":[{\"endTime\":\"20:11\",\"startTime\":\"09:11\"}]},\"adGroupScheduleConfig\":{\"periodic\":false,\"excludeDate\":[\"\"],\"periodInfo\":\"{}\"},\"eventConfig\":{},\"crowdTargetingList\":[{\"bizDate\":\"20191226\",\"principalType\":\"USER_ID\",\"crowdDesc\":\"\",\"crowdId\":\"191226200530888\",\"crowdName\":\"ZGX_已映射_会员域1W用户(清洗后)\",\"crowdSource\":\"FILE\",\"crowdStatus\":1,\"crowdTotalCount\":10000,\"crowdType\":\"FILE\",\"dataUpdateDate\":\"20191226\",\"dataUpdateType\":\"ONCE\",\"expireDate\":\"20201220\",\"fileContentList\":[],\"fileName\":\"会员域人群(清洗后)10000个用户.txt\",\"gmtCreate\":\"2019-12-26 20:05:51\",\"gmtModified\":\"2019-12-26 20:05:51\",\"gmtModifier\":\"[email protected]\",\"gmtModifierId\":\"[email protected]\",\"groupConditions\":[],\"operatorId\":\"[email protected]\",\"operatorName\":\"[email protected]\",\"ossPath\":\"\",\"parentTaskName\":\"\",\"projectName\":\"\",\"sceneId\":\"DEFAULT\",\"storeType\":\"\",\"tableName\":\"\",\"tenantId\":\"DANAW3ID\",\"userIdColumn\":\"\",\"userIdType\":\"USER_ID\",\"userProperty\":\"\",\"useSceneType\":\"TARGETING\"}],\"fatigueLimitVOList\":[],\"name\":\"任务名称_20200102194954313\",\"priority\":1,\"ruleTargetingVOList\":[],\"terminalTargetingList\":[],\"contentVOList\":[{\"creativeVOList\":[{\"name\":\"测试\",\"creativeType\":\"TEXT\",\"creativeDetailVoList\":[{\"language\":\"en_US\",\"defaultOrNot\":true,\"variables\":\"{\\\"text\\\":\\\"内容\\\",\\\"title\\\":\\\"标题\\\"}\"}],\"extendInfo\":\"{\\\"isEmpty\\\":false}\",\"shunt\":100}]}],\"bizType\":\"content_task\",\"adgroupAwardInfoVO\":{}}}";

2     String cuo = "{\"adGroupVO\":{\"contentVOList\":[{\"creativeVOList\":[{\"shunt\":100,\"creativeType\":\"TEXT\",\"creativeDetailVoList\":[{\"variables\":\"{\\\"text\\\":\\\"内容\\\",\\\"title\\\":\\\"标题\\\"}\",\"defaultOrNot\":true,\"language\":\"en_US\"}],\"name\":\"测试\",\"extendInfo\":\"{\\\"isEmpty\\\":false}\"}]}],\"bizType\":\"content_task\",\"campaignId\":\"CAMPAIGN201912101000004559\",\"adGroupScheduleConfig\":{\"excludeDate\":[\"\"],\"periodic\":false,\"periodInfo\":\"{}\"},\"priority\":1,\"eventConfig\":{},\"terminalTargetingList\":[],\"adGroupTimeConfigVO\":{\"endDate\":\"2020-01-04\",\"adGroupTimeConfigDetailVoList\":[{\"startTime\":\"09:11\",\"endTime\":\"20:11\"}],\"startDate\":\"2020-01-02\"},\"fatigueLimitVOList\":[],\"name\":\"任务名称_20200102194954313\",\"deliveryScene\":\"default\",\"adgroupAwardInfoVO\":{},\"adGroupChannel\":{\"resourceCode\":\"TEMPLATE_CODE\",\"channelType\":\"SMS\"},\"ruleTargetingVOList\":[],\"crowdTargetingList\":[{\"fileName\":\"会员域人群(清洗后)10000个用户.txt\",\"gmtModified\":\"2019-12-26 20:05:51\",\"parentTaskName\":\"\",\"bizDate\":\"20191226\",\"userIdType\":\"USER_ID\",\"userProperty\":\"\",\"crowdSource\":\"FILE\",\"crowdId\":\"191226200530888\",\"crowdType\":\"FILE\",\"operatorName\":\"[email protected]\",\"tableName\":\"\",\"userIdColumn\":\"\",\"sceneId\":\"DEFAULT\",\"expireDate\":\"20201220\",\"crowdDesc\":\"\",\"operatorId\":\"[email protected]\",\"crowdName\":\"ZGX_已映射_会员域1W用户(清洗后)\",\"ossPath\":\"\",\"crowdStatus\":1,\"storeType\":\"\",\"groupConditions\":[],\"gmtModifierId\":\"[email protected]\",\"crowdTotalCount\":10000,\"gmtCreate\":\"2019-12-26 20:05:51\",\"fileContentList\":[],\"dataUpdateType\":\"ONCE\",\"gmtModifier\":\"[email protected]\",\"useSceneType\":\"TARGETING\",\"dataUpdateDate\":\"20191226\",\"tenantId\":\"DANAW3ID\",\"projectName\":\"\",\"principalType\":\"USER_ID\"}]}}";

二、对比

2.1、思路:2个顺序不一样的json字符串 通过 JSONObject.parse(dui)  打印出来后 顺序就是一摸一样的了(前提:key都一样)  然后再通过一个字符串查询就行了

2.2、实现:

2.3、打印后

1 {"adGroupVO":{"contentVOList":[{"creativeVOList":[{"shunt":100,"creativeType":"TEXT","creativeDetailVoList":[{"variables":"{\"text\":\"内容\",\"title\":\"标题\"}","defaultOrNot":true,"language":"en_US"}],"name":"测试","extendInfo":"{\"isEmpty\":false}"}]}],"bizType":"content_task","campaignId":"CAMPAIGN201912101000004559","adGroupScheduleConfig":{"excludeDate":[""],"periodic":false,"periodInfo":"{}"},"priority":1,"eventConfig":{},"terminalTargetingList":[],"adGroupTimeConfigVO":{"endDate":"2020-01-04","adGroupTimeConfigDetailVoList":[{"startTime":"09:11","endTime":"20:11"}],"startDate":"2020-01-02"},"fatigueLimitVOList":[],"name":"任务名称_20200102194954313","deliveryScene":"default","adgroupAwardInfoVO":{},"adGroupChannel":{"resourceCode":"TEMPLATE_CODE","channelType":"SMS"},"ruleTargetingVOList":[],"crowdTargetingList":[{"fileName":"会员域人群(清洗后)10000个用户.txt","gmtModified":"2019-12-26 20:05:51","parentTaskName":"","bizDate":"20191226","userIdType":"USER_ID","userProperty":"","crowdSource":"FILE","crowdId":"191226200530888","crowdType":"FILE","operatorName":"[email protected]","tableName":"","userIdColumn":"","sceneId":"DEFAULT","expireDate":"20201220","crowdDesc":"","operatorId":"[email protected]","crowdName":"ZGX_已映射_会员域1W用户(清洗后)","ossPath":"","crowdStatus":1,"storeType":"","groupConditions":[],"gmtModifierId":"[email protected]","crowdTotalCount":10000,"gmtCreate":"2019-12-26 20:05:51","fileContentList":[],"dataUpdateType":"ONCE","gmtModifier":"[email protected]","useSceneType":"TARGETING","dataUpdateDate":"20191226","tenantId":"DANAW3ID","projectName":"","principalType":"USER_ID"}]}} 

2 {"adGroupVO":{"contentVOList":[{"creativeVOList":[{"shunt":100,"creativeType":"TEXT","creativeDetailVoList":[{"variables":"{\"text\":\"内容\",\"title\":\"标题\"}","defaultOrNot":true,"language":"en_US"}],"name":"测试","extendInfo":"{\"isEmpty\":false}"}]}],"bizType":"content_task","campaignId":"CAMPAIGN201912101000004559","adGroupScheduleConfig":{"excludeDate":[""],"periodic":false,"periodInfo":"{}"},"priority":1,"eventConfig":{},"terminalTargetingList":[],"adGroupTimeConfigVO":{"endDate":"2020-01-04","adGroupTimeConfigDetailVoList":[{"startTime":"09:11","endTime":"20:11"}],"startDate":"2020-01-02"},"fatigueLimitVOList":[],"name":"任务名称_20200102194954313","deliveryScene":"default","adgroupAwardInfoVO":{},"adGroupChannel":{"resourceCode":"TEMPLATE_CODE","channelType":"SMS"},"ruleTargetingVOList":[],"crowdTargetingList":[{"fileName":"会员域人群(清洗后)10000个用户.txt","gmtModified":"2019-12-26 20:05:51","parentTaskName":"","bizDate":"20191226","userIdType":"USER_ID","userProperty":"","crowdSource":"FILE","crowdId":"191226200530888","crowdType":"FILE","operatorName":"[email protected]","tableName":"","userIdColumn":"","sceneId":"DEFAULT","expireDate":"20201220","crowdDesc":"","operatorId":"[email protected]","crowdName":"ZGX_已映射_会员域1W用户(清洗后)","ossPath":"","crowdStatus":1,"storeType":"","groupConditions":[],"gmtModifierId":"[email protected]","crowdTotalCount":10000,"gmtCreate":"2019-12-26 20:05:51","fileContentList":[],"dataUpdateType":"ONCE","gmtModifier":"[email protected]","useSceneType":"TARGETING","dataUpdateDate":"20191226","tenantId":"DANAW3ID","projectName":"","principalType":"USER_ID"}]}}

原文地址:https://www.cnblogs.com/danhuai/p/12141765.html

时间: 2024-11-06 17:12:00

【Java】+【JSON】+对比两个json对象是否完全一样的相关文章

如何在JAVA中比较两个String对象

问题 最近写程序的时候,遇到了需要比较两个 String 对象是否相等的情况,我习惯性的写了形如if(a == "a"){}的语句,IDEA 跳出警告,内容如下: String values are compared using '==', not 'equals()'. 也就是说我刚刚那句话应该写成if(a.equals("a")){}才对,果然不再标红了. 说明 那么,为什么会这样呢?==和equals()分别是什么效果呢? 对于基本数据类型byte(字节型).

Java中对比两个对象中属性值[反射、注解]

在Java中通常要比较两个对象在修改前与修改后的值是否相同,一般我们采用的是反射技术获取对象的get方法[或其他的方法]获取值并做比较.如果系统将修改的属性名称也显示出来,这样就能更直观的显示类中的哪一个属性的值被修改了.然后Java中只能获取属性的名称,也就是英文标识的属性名,但是一般我们都会在属性后面添加属性的注释,但是Java不提供注释获取的方法.所以我们只能使用另外一种方式来将属性和属性注释关联起来,这就是Java中的@Annotation. public static  Map<Str

比较任意两个JSON串是否相等(比较对象是否相等)JAVA版

废话少说,直接入题. 在面向对象语言中,经常会比较两个对象是否相等,而比较的大多是实体类实例,也就是封装数据的那些类实例,或者是Map.List互相嵌套成的复杂数据结构. 比较对象是否相等,常见的思路是重写equals方法,但鉴于对象的种类多变,嵌套层次复杂,仅仅靠重写equals是很难实现的. 小菜的思路是可以把对象序列化,由于这些对象均是用来表达数据结构,因此可以直接转换成JSON字符串,用字符串来描述数据结构,避免实现Serializable接口.将对象序列化成字符串后,比较是否相等就相对

两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)

本篇文章主要介绍了"两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)",主要涉及到两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)方面的内容,对于两款JSON类库Jackson与JSON-lib的性能对比(新增第三款测试)感兴趣的同学可以参考一下. 近日做一些性能优化工作,在挑选JSON类库时,发现除了一般常用的JSON-lib外,还有一款号称性能最快的JSON处理器Jackson,于是用上了刚学会的JMeter,对这两个类库进行

XML在java或.NET中转为Json的数组或对象数据时的处理

XML在java和.NET中转为Json数据时会出现这样一个问题,当节点中只有一个节点数据时是转换为对象,有多个节点则是转为数组,为了应对这个问题我们需要在解析的时候进行如下的处理,不然就会报异常: 数据1:数组格式 "Field": [                    {                        "@name": "APPROVE",                        "@title"

java中对象转json,json转list,json转map

在IDEA中的springboot项目里写的一个测试例子,新建User类,四个字段,Long id , String password,String userName,int age; 以及带参构造,toString方法 在pom.xml中添加 net.sf.json-lib  依赖: 1 <!-- 对象转json依赖包 --> 2 <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --> 3 <

json字符串转成 json对象 json对象转换成java对象

import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject; 依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version></dependency> String r

JAVA中 Map转换实体类对象简单实现 JSON

原文链接:https://blog.csdn.net/qq_23140197/article/details/86503875   (侵删) 开发的过程中往往依赖的表过多直接按Map来传递数值,某些场景需要把Map转换为实体类,这里贴一个最简洁的方法,依赖阿里的FastJSon. maven引入 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId>

Java:JSON解析工具-org.json

一.简介 org.json是Java常用的Json解析工具,主要提供JSONObject和JSONArray类,现在就各个类的使用解释如下. 二.准备 1.在使用org.json之前,我们应该先从该网址https://github.com/douglascrockford/JSON-java下载org.json源码,并将源码其加入到Eclipse中,即可调用. 2.查看相关的API文档,访问:https://github.com/douglascrockford/JSON-java. https