使用 Jackson 比較兩個 JSON 對象

Data,Jackson
Remote
0
10:23 PM · Nov 30 ,2025

1. 概述

在本教程中,我們將探討如何使用 Jackson(一個用於 Java 的 JSON 處理庫)比較兩個 JSON 對象。

2. Maven 依賴

首先,讓我們添加 jackson-databind Maven 依賴:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>

3. 使用 Jackson 進行兩個 JSON 對象比較

我們將使用 ObjectMapper 類來讀取對象為 JsonNode

讓我們創建一個 ObjectMapper

ObjectMapper mapper = new ObjectMapper();

3.1. 比較兩個簡單的 JSON 對象

讓我們首先使用 JsonNode.equals 方法。 equals() 方法執行一個完整的(深層)比較。

假設我們有一個 JSON 字符串定義為 s1 變量:

{
    "employee":
    {
        "id": "1212",
        "fullName": "John Miles",
        "age": 34
    }
}

我們想用它與另一個 JSON 進行比較,s2

{   
    "employee":
    {
        "id": "1212",
        "age": 34,
        "fullName": "John Miles"
    }
}

讓我們讀取輸入 JSON 作為 JsonNode 並進行比較:

assertEquals(mapper.readTree(s1), mapper.readTree(s2));

重要的是要注意,即使輸入 JSON 變量 s1s2 的屬性順序不同,equals() 方法會忽略順序並將其視為相等。

3.2. 比較具有嵌套元素的兩個 JSON 對象

接下來,我們將看到如何比較具有嵌套元素的兩個 JSON 對象。

讓我們從一個 JSON 定義為 s1 變量開始:

{ 
    "employee":
    {
        "id": "1212",
        "fullName":"John Miles",
        "age": 34,
        "contact":
        {
            "email": "[email protected]",
            "phone": "9999999999"
        }
    }
}

我們可以看到,JSON 包含一個嵌套元素 contact

我們想用它與另一個 JSON 進行比較,定義為 s2

{
    "employee":
    {
        "id": "1212",
        "age": 34,
        "fullName": "John Miles",
        "contact":
        {
            "email": "[email protected]",
            "phone": "9999999999"
        }
    }
}

讓我們讀取輸入 JSON 作為 JsonNode 並進行比較:

assertEquals(mapper.readTree(s1), mapper.readTree(s2));

再次,我們應該注意到,equals() 也可以比較具有嵌套元素的兩個輸入 JSON 對象。

3.3. 比較包含列表元素的兩個 JSON 對象

同樣,我們也可以比較包含列表元素的兩個 JSON 對象。

讓我們考慮一個 JSON 定義為 s1 變量:

{
    "employee":
    {
        "id": "1212",
        "fullName": "John Miles",
        "age": 34,
        "skills": ["Java", "C++", "Python"]
    }
}

我們正在用它與另一個 JSON 進行比較,s2

{
    "employee":
    {
        "id": "1212",
        "age": 34,
        "fullName": "John Miles",
        "skills": ["Java", "C++", "Python"] 
    } 
}

讓我們讀取輸入 JSON 作為 JsonNode 並進行比較:

assertEquals(mapper.readTree(s1), mapper.readTree(s2));

重要的是要知道,兩個列表元素僅在它們具有相同的值以及在相同順序中進行比較。

4. Compare Two JSON Objects With a Custom Comparator

works quite well in most cases. Jackson also provides to configure a custom Java object.

Let’s understand how to use a custom .

4.1. Custom Comparator to Compare Numeric Values

Let’s look at how to use a custom to compare two JSON elements having numeric values.

We’ll use this JSON as input s1:

{
    "name": "John",
    "score": 5.0
}

Let’s compare with another JSON defined as s2:

{
    "name": "John",
    "score": 5
}

We need to observe that the values of attribute in inputs and are not the same.

Let’s read the input JSON as and compare:

JsonNode actualObj1 = mapper.readTree(s1);
JsonNode actualObj2 = mapper.readTree(s2);

assertNotEquals(actualObj1, actualObj2);

Notice that the two objects are not equal. The standard method considers values 5.0 and 5 as different.

However, we can use a custom to compare values 5 and 5.0 and treat them as equal.

Let’s first create a to compare two objects:

public class NumericNodeComparator implements Comparator<JsonNode> 
{
    @Override
    public int compare(JsonNode o1, JsonNode o2)
    {
        if (o1.equals(o2)){
           return 0;
        }
        if ((o1 instanceof NumericNode) &&& (o2 instanceof NumericNode)){
            Double d1 = ((NumericNode) o1).asDouble();
            Double d2 = ((NumericNode) o2).asDouble(); 
            if (d1.compareTo(d2) == 0) {
               return 0;
            }
        }
        return 1;
    }
}

Next, let’s see how to use this :

NumericNodeComparator cmp = new NumericNodeComparator();
assertTrue(actualObj1.equals(cmp, actualObj2));

4.2. Custom Comparator to Compare Text Values

Let’s see another example of a custom for a case-insensitive comparison of two JSON values.

We’ll use this JSON as input s1:

{
    "name": "john", 
    "score": 5 
}

Let’s compare with another JSON defined as s2:

{ 
    "name": "JOHN", 
    "score": 5 
}

As we can see, the attribute is lowercase in input and uppercase in .

Let’s first create a to compare two objects:

public class TextNodeComparator implements Comparator<JsonNode> 
{
    @Override
    public int compare(JsonNode o1, JsonNode o2) {
        if (o1.equals(o2)) {
            return 0;
        }
        if ((o1 instanceof TextNode) &&& (o2 instanceof TextNode)) {
            String s1 = ((TextNode) o1).asText();
            String s2 = ((TextNode) o2).asText();
            if (s1.equalsIgnoreCase(s2)) {
                return 0;
            }
        }
        return 1;
    }
}

Let’s see how to compare and using :

JsonNode actualObj1 = mapper.readTree(s1);
JsonNode actualObj2 = mapper.readTree(s2);

TextNodeComparator cmp = new TextNodeComparator();

assertNotEquals(actualObj1, actualObj2);
assertTrue(actualObj1.equals(cmp, actualObj2));

Finally, we can see using a custom comparator object while comparing two JSON objects can be very useful when the input JSON element value is not exactly the same but we still want to treat them as equal.

5. 結論

在本文中,我們看到了如何使用 Jackson 比較兩個 JSON 對象以及使用自定義比較器

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.