我有 2 个带有嵌套元素的大型 JSON,我需要找到这些 json 的差异。每个 Json 代表不同日期来自 LDAP 的数据。我正在研究 Jackson 的 JsonNode 类。该类的 Equals 方法可以比较 2 个 JsonNode,但它不返回差异。
感谢任何有关获取 2 个 JSON 差异的最佳方法的意见。如果有人有相同的示例代码,那就太好了。谢谢。
{
"id": "_6MrXgEx7Ee6GUol3yT5KIw",
"name": "test",
"fullName": "I_EXT_TEST",
"members": [
{
"mail": null,
"roles": [
{
"id": null,
"name": "Author"
},
{
"id": null,
"name": "Commenter"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=aaaaaaaaaa"
},
{
"mail": null,
"roles": [
{
"id": null,
"name": "Author"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=bbbbbbbbbbb"
},
{
"mail": null,
"roles": [
{
"id": null,
"name": "Commenter"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=ccccccc"
},
{
"mail": null,
"roles": [
{
"id": null,
"name": "Commenter"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=ddddddd"
},
]
}
{
"id": "_6MrXgEx7Ee6GUol3yT5KIw",
"name": "test",
"fullName": "I_EXT_TEST",
"members": [
{
"mail": null,
"roles": [
{
"id": null,
"name": "Commenter"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=ccccccc"
},
{
"mail": null,
"roles": [
{
"id": null,
"name": "Commenter"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=ddddddd"
},
]
}
"members": [
{
"mail": null,
"roles": [
{
"id": null,
"name": "Author"
},
{
"id": null,
"name": "Commenter"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=aaaaaaaaaa"
},
{
"mail": null,
"roles": [
{
"id": null,
"name": "Author"
}
],
"dng_ID": "[email protected]",
"ad_ID": "CN=bbbbbbbbbbb"
}
]
}
JSON 库 Jossons & Josson 对两个数据集进行设置操作。
https://github.com/octomix/josson
反序列化
Jossons jossons = new Jossons();
jossons.putDataset("JSON1", Josson.fromJsonString(
"{" +
" \"id\": \"_6MrXgEx7Ee6GUol3yT5KIw\"," +
" \"name\": \"test\"," +
" \"fullName\": \"I_EXT_TEST\"," +
" \"members\": [" +
" {" +
" \"mail\": null," +
" \"roles\": [" +
" {" +
" \"id\": null," +
" \"name\": \"Author\"" +
" }," +
" {" +
" \"id\": null," +
" \"name\": \"Commenter\"" +
" }" +
" ]," +
" \"dng_ID\": \"[email protected]\"," +
" \"ad_ID\": \"CN=aaaaaaaaaa\"" +
" }," +
" {" +
" \"mail\": null," +
" \"roles\": [" +
" {" +
" \"id\": null," +
" \"name\": \"Author\"" +
" }" +
" ]," +
" \"dng_ID\": \"[email protected]\"," +
" \"ad_ID\": \"CN=bbbbbbbbbbb\"" +
" }," +
" {" +
" \"mail\": null," +
" \"roles\": [" +
" {" +
" \"id\": null," +
" \"name\": \"Commenter\"" +
" }" +
" ]," +
" \"dng_ID\": \"[email protected]\"," +
" \"ad_ID\": \"CN=ccccccc\"" +
" }," +
" {" +
" \"mail\": null," +
" \"roles\": [" +
" {" +
" \"id\": null," +
" \"name\": \"Commenter\"" +
" }" +
" ]," +
" \"dng_ID\": \"[email protected]\"," +
" \"ad_ID\": \"CN=ddddddd\"" +
" }" +
" ]" +
"}"));
jossons.putDataset("JSON2", Josson.fromJsonString(
"{" +
" \"id\": \"_6MrXgEx7Ee6GUol3yT5KIw\"," +
" \"name\": \"test\"," +
" \"fullName\": \"I_EXT_TEST\"," +
" \"members\": [" +
" {" +
" \"mail\": null," +
" \"roles\": [" +
" {" +
" \"id\": null," +
" \"name\": \"Commenter\"" +
" }" +
" ]," +
" \"dng_ID\": \"[email protected]\"," +
" \"ad_ID\": \"CN=ccccccc\"" +
" }," +
" {" +
" \"mail\": null," +
" \"roles\": [" +
" {" +
" \"id\": null," +
" \"name\": \"Commenter\"" +
" }" +
" ]," +
" \"dng_ID\": \"[email protected]\"," +
" \"ad_ID\": \"CN=ddddddd\"" +
" }" +
" ]" +
"}"));
左减右
JsonNode node = jossons.evaluateQuery("JSON1 <-< JSON2");
System.out.println(node.toPrettyString());
输出
{
"members" : [ {
"mail" : null,
"roles" : [ {
"id" : null,
"name" : "Author"
}, {
"id" : null,
"name" : "Commenter"
} ],
"dng_ID" : "[email protected]",
"ad_ID" : "CN=aaaaaaaaaa"
}, {
"mail" : null,
"roles" : [ {
"id" : null,
"name" : "Author"
} ],
"dng_ID" : "[email protected]",
"ad_ID" : "CN=bbbbbbbbbbb"
} ]
}