以递归方式比较两个json节点并在java中打印distinct值

时间:2016-04-27 11:39:36

标签: java json

我想比较java中的两个大json节点,如果它们不同,我需要打印两个节点中不同的值。 如果节点不相等但JsonNode.equals()返回false,则它不会告诉特定的不同值。

感谢。

代码示例:

    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));

        }


    }

2 个答案:

答案 0 :(得分:2)

检查Parse a JSON Object With an Undetermined Amount of Child Nodes

您可以获取一个节点的子节点列表。然后遍历子项,获取键/值并使用键比较第二个节点的每个元素。

答案 1 :(得分:1)

我建议使用文件比较器并逐行比较。如果您还关注节点中元素的顺序,这将有效。如果不考虑顺序,则首先使用JsonNode.equals()检查相等性,并且仅当节点不相等时,使用文件比较器来打印差异。