Java JSON string comparison

Source: Internet
Author: User

 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<Entry<String,JsonNode>> FIELDS1 =Node2.fields (); Map<String,JsonNode> FIELDS2 =GetFields (Node1); BooleanFlag =true;  while(Fields1.hasnext ()) {Entry<String,JsonNode> 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<JsonNode> it1 =node1.elements ();  while(It1.hasnext ()) {BooleanFlag =false; Jsonnode node=It1.next (); Iterator<JsonNode> it2 =node2.elements ();  while(It2.hasnext ()) {if(Comparejsonnode (node, It2.next ())) {flag=true;  Break; }            }            if(!flag)return false; }        return true; }         Public Static BooleanComparefieldnames (iterator<string> it1, iterator<string>it2) {List<String> NameList1 =NewArraylist<string>(); List<String> NameList2 =NewArraylist<string>();  while(It1.hasnext ()) {Namelist1.add (It1.next ()); }         while(It2.hasnext ()) {Namelist2.add (It2.next ()); }        returnNamelist1.containsall (NAMELIST2) &&Namelist2.containsall (NAMELIST1); }         Public StaticMap<string, jsonnode>GetFields (Jsonnode node) {Iterator<Entry<String,JsonNode>> fields =Node.fields (); Map<string, jsonnode> FieldMap =NewHashmap<string, jsonnode>();  while(Fields.hasnext ()) {Entry<String,JsonNode> field =Fields.next ();        Fieldmap.put (Field.getkey (), Field.getvalue ()); }        returnFieldMap; }}

Java JSON string comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.