java json 比较_Java json字符串对比

public classJsonUtil {public static booleancompareJsonText(String str1, String str2) {returncompareJsonNode(JsonUtil.readTree(str1), JsonUtil.readTree(str2));

}public static booleancompareJsonNode(JsonNode node1, JsonNode node2) {if(node1.isObject()) {if(!node2.isObject()) return false;if(!compareFieldNames(node1.fieldNames(), node2.fieldNames()))return false;

Iterator> fields1 =node2.fields();

Map fields2 =getFields(node1);boolean flag = true;while(fields1.hasNext()){

Entry field1 =fields1.next();

JsonNode field2=fields2.get(field1.getKey());if(!compareJsonNode(field1.getValue(), field2))

flag= false;

}returnflag;

}else if(node1.isArray()) {if(!node2.isArray()) return false;returncompareArrayNode(node1, node2);

}else{returnnode1.toString().equals(node2.toString());

}

}public static booleancompareArrayNode(JsonNode node1, JsonNode node2){

Iterator it1 =node1.elements();while(it1.hasNext()){boolean flag = false;

JsonNode node=it1.next();

Iterator it2 =node2.elements();while(it2.hasNext()){if(compareJsonNode(node, it2.next())){

flag= true;break;

}

}if(!flag)return false;

}return true;

}public static boolean compareFieldNames(Iterator it1, Iteratorit2) {

List nameList1 = new ArrayList();

List nameList2 = new ArrayList();while(it1.hasNext()){

nameList1.add(it1.next());

}while(it2.hasNext()){

nameList2.add(it2.next());

}return nameList1.containsAll(nameList2) &&nameList2.containsAll(nameList1);

}public static MapgetFields(JsonNode node) {

Iterator> fields =node.fields();

Map fieldMap = new HashMap();while(fields.hasNext()){

Entry field =fields.next();

fieldMap.put(field.getKey(), field.getValue());

}returnfieldMap;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值