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

Posted 淡怀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\\":\\"zhangguoxiang@sitTest.com\\",\\"gmtModifierId\\":\\"zhangguoxiang@sitTest.com\\",\\"groupConditions\\":[],\\"operatorId\\":\\"zhangguoxiang@sitTest.com\\",\\"operatorName\\":\\"zhangguoxiang@sitTest.com\\",\\"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\\":\\"zhangguoxiang@sitTest.com\\",\\"tableName\\":\\"\\",\\"userIdColumn\\":\\"\\",\\"sceneId\\":\\"DEFAULT\\",\\"expireDate\\":\\"20201220\\",\\"crowdDesc\\":\\"\\",\\"operatorId\\":\\"zhangguoxiang@sitTest.com\\",\\"crowdName\\":\\"ZGX_已映射_会员域1W用户(清洗后)\\",\\"ossPath\\":\\"\\",\\"crowdStatus\\":1,\\"storeType\\":\\"\\",\\"groupConditions\\":[],\\"gmtModifierId\\":\\"zhangguoxiang@sitTest.com\\",\\"crowdTotalCount\\":10000,\\"gmtCreate\\":\\"2019-12-26 20:05:51\\",\\"fileContentList\\":[],\\"dataUpdateType\\":\\"ONCE\\",\\"gmtModifier\\":\\"zhangguoxiang@sitTest.com\\",\\"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":"zhangguoxiang@sitTest.com","tableName":"","userIdColumn":"","sceneId":"DEFAULT","expireDate":"20201220","crowdDesc":"","operatorId":"zhangguoxiang@sitTest.com","crowdName":"ZGX_已映射_会员域1W用户(清洗后)","ossPath":"","crowdStatus":1,"storeType":"","groupConditions":[],"gmtModifierId":"zhangguoxiang@sitTest.com","crowdTotalCount":10000,"gmtCreate":"2019-12-26 20:05:51","fileContentList":[],"dataUpdateType":"ONCE","gmtModifier":"zhangguoxiang@sitTest.com","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":"zhangguoxiang@sitTest.com","tableName":"","userIdColumn":"","sceneId":"DEFAULT","expireDate":"20201220","crowdDesc":"","operatorId":"zhangguoxiang@sitTest.com","crowdName":"ZGX_已映射_会员域1W用户(清洗后)","ossPath":"","crowdStatus":1,"storeType":"","groupConditions":[],"gmtModifierId":"zhangguoxiang@sitTest.com","crowdTotalCount":10000,"gmtCreate":"2019-12-26 20:05:51","fileContentList":[],"dataUpdateType":"ONCE","gmtModifier":"zhangguoxiang@sitTest.com","useSceneType":"TARGETING","dataUpdateDate":"20191226","tenantId":"DANAW3ID","projectName":"","principalType":"USER_ID"}]}}

以上是关于Java+JSON+对比两个json对象是否完全一样的主要内容,如果未能解决你的问题,请参考以下文章

String转json,多层json解析,并使用JsonElement对比两个json对象的值是否相等

如何判断两个json对象相等

对比两个对象的属性和值是否完全相同

JSON与XML对比MIMEJSON对象JSON数组

FastJsonJacksonGson进行Java对象转换Json的细节处理

Java中json工具对比分析