2016-04-27 30 views
-2

Javaの2つの大きなjsonノードを比較したいのですが、それらが異なる場合、両方のノードで別々の値を出力する必要があります。 JsonNode.equals()は、ノードが等しくないが、特定の個別値を通知しない場合はfalseを返します。2つのjsonノードを再帰的に比較して、別の値をJavaで出力する

ありがとうございました。

コード例:

import org.codehaus.jackson.JsonNode; 
    import org.codehaus.jackson.map.ObjectMapper; 
    import org.codehaus.jackson.map.util.JSONPObject; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import java.io.IOException; 
    import java.util.Iterator; 
    import java.util.Map; 


    public class TestJsonComp 
    { 
     static String json = "{\n" + 
      " \"d\" :\n" + 
      " {\n" + 
      " \"results\" :\n" + 
      " [\n" + 
      "  {\n" + 
      "  \"__metadata\" :\n" + 
      "  {\n" + 
      "   \"id\" :    \"   ##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" + 
    "   \"uri\" : \"##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" + 
    "   \"type\" : \".submitResponse\",\n" + 
    "   \"etag\" : \"W/\\\"##RequisitionUniqueName##\\\"\"\n" + 
    "  },\n" + 
    "  \"status\" : \"ERROR\",\n" + 
    "  \"uniqueName\" : \"##RequisitionUniqueName##\",\n" + 
    "  \"errors\" :\n" + 
    "  {\n" + 
    "   \"results\" :\n" + 
    "   [\n" + 
    "   {\n" + 
    "    \"__metadata\" :\n" + 
    "    {\n" + 
    "    \"id\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" + 
    "    \"uri\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" + 
    "    \"type\" : \".errors\",\n" + 
    "    \"etag\" : \"W/\\\"0\\\"\"\n" + 
    "    },\n" + 
    "    \"id\" : 0,\n" + 
    "    \"error_description\" : \"title.\"\n" + 
    "   },\n" + 
    "   {\n" + 
    "    \"__metadata\" :\n" + 
    "    {\n" + 
    "    \"id\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" + 
    "    \"uri\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" + 
    "    \"type\" : \".errors\",\n" + 
    "    \"etag\" : \"W/\\\"1\\\"\"\n" + 
    "    },\n" + 
    "    \"id\" : 1,\n" + 
    "    \"error_description\" : \"Line item 1 must be set.\"\n" + 
    "   }\n" + 
    "   ]\n" + 
    "  }\n" + 
    "  }\n" + 
    " ]\n" + 
    " }\n" + 
    "}"; 

     static String jsonCompare = "{\n" + 
    " \"d\" :\n" + 
    " {\n" + 
    " \"results\" :\n" + 
    " [\n" + 
    "  {\n" + 
    "  \"__metadata\" :\n" + 
    "  {\n" + 
    "   \"id\" : \"##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" + 
    "   \"uri\" : \"##ServiceRoot##/Submit('##RequisitionUniqueName##')\",\n" + 
    "   \"type\" : \".submitResponse\",\n" + 
    "   \"etag\" : \"W/\\\"##RequisitionUniqueName##\\\"\"\n" + 
    "  },\n" + 
    "  \"status\" : \"ERROR\",\n" + 
    "  \"uniqueName\" : \"##RequisitionUniqueName##\",\n" + 
    "  \"errors\" :\n" + 
    "  {\n" + 
    "   \"results\" :\n" + 
    "   [\n" + 
    "   {\n" + 
    "    \"__metadata\" :\n" + 
    "    {\n" + 
    "    \"id\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" + 
    "    \"uri\" : \"##ServiceRoot##/RequisitionErrors(0)\",\n" + 
    "    \"type\" : \".errors\",\n" + 
    "    \"etag\" : \"W/\\\"0\\\"\"\n" + 
    "    },\n" + 
    "    \"id\" : 0,\n" + 
    "    \"error_description\" : \"title.\"\n" + 
    "   },\n" + 
    "   {\n" + 
    "    \"__metadata\" :\n" + 
    "    {\n" + 
    "    \"id\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" + 
    "    \"uri\" : \"##ServiceRoot##/RequisitionErrors(1)\",\n" + 
    "    \"type\" : \".errors\",\n" + 
    "    \"etag\" : \"W/\\\"1\\\"\"\n" + 
    "    },\n" + 
    "    \"id\" : 1,\n" + 
    "    \"error_descriptio\" : \"Line item 1 must be set.\"\n" + 
    "   }\n" + 
    "   ]\n" + 
    "  }\n" + 
    "  }\n" + 
    " ]\n" + 
    " }\n" + 
    "}"; 


     public static void main (String args []) throws IOException, JSONException 
     { 
      ObjectMapper objectMapper = new ObjectMapper(); 
      JsonNode n1 = objectMapper.readTree(json); 
      JsonNode n2 = objectMapper.readTree(jsonCompare); 
      System.out.print(n1.equals(n2)); 

     } 


    } 
+1

あなたのコードとjson構造と一緒にそれを行う試みを私たちに教えてください – Ricardo

答えて

1

ファイルコンパレータを使用し、行ごとに比較することをお勧めします。これは、ノード内の要素の順序にも気をつけている場合に機能します。順序が考慮されていない場合は、最初にJsonNode.equals()を使用して等価性をチェックし、ノードが等しくない場合にのみ、ファイル比較器を使用して差異を出力します。

関連する問題