使用JSONAssert查找json diff失败 [英] finding json diff fails using JSONAssert

查看:146
本文介绍了使用JSONAssert查找json diff失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用Jackson来查找JSON差异,但它没有给出详细的错误消息.

I was hoping to use Jackson to find JSON diff but it does not give detailed error messages.

所以我尝试使用JSOnAssert查找两个JSON字符串之间的差异.

So I tried using JSOnAssert to find the diff between two JSON strings.

JSONAssert.assertEquals(expectedJsonResponse, actualJsonResponse, false);

可悲的是,它似乎没有正确匹配,并没有像示例中那样提供详细的错误消息.如果使用过,请您澄清一下吗?

Sadly, it does not appear to match correctly and give the detailed error messages as in the examples. If you have used it, Can you please clarify?

java.lang.AssertionError: data[0] Could not find match for element {"errors":[{"httpStatus":"BAD_REQUEST","personId":null,"details":"User ID [UNKNOWN]. Invalid ID: NONSENSE"}],"successfulIds":["A0","B1","C3"]}
at org.skyscreamer.jsonassert.JSONAssert.assertEquals(JSONAssert.java:222)

实际JSON:

{"_links":{"self":{"href":"https://myserver.com:1000/api/person/upload? myCsvFile={myCsvFile}","templated":true}},"data":[{"successfulIds":["A0","XYZ","C3"],"errors":[{"personId":null,"httpStatus":"BAD_REQUEST","details":"User ID [UNKNOWN]. Invalid ID: NONSENSE"}]}]}

期望的JSON:

{
    "_links": {
        "self": {
            "href": "https://myserver.com:1000/api/person/upload?myCsvFile={myCsvFile}",
            "templated": true
        }
    },
    "data": [
        {
            "successfulIds": [
                "A0",
                "B1",
                "C3"
            ],
            "errors": [
                {
                    "personId": null,
                    "httpStatus": "BAD_REQUEST",
                    "details": "User ID [UNKNOWN]. Invalid ID: NONSENSE"
                }
            ]
        }
    ]
}

推荐答案

我试图通过电子邮件将地址发送到 http://jsonassert.skyscreamer.org/但得到了

I tried to email the address at http://jsonassert.skyscreamer.org/ but got a

以下发送至jsonassert-dev@skyscreamer.org的消息为 无法交付.问题的原因: 5.1.0-未知地址错误550-"5.1.1您尝试访问的电子邮件帐户不存在

The following message to jsonassert-dev@skyscreamer.org was undeliverable. The reason for the problem: 5.1.0 - Unknown address error 550-"5.1.1 The email account that you tried to reach does not exist

所以我尝试了ZJsonPatch.我喜欢这样的事实,即与Jackson一起使用时,成员的顺序无关紧要.换句话说,我首先尝试使用Jackson来检查是否相等.杰克逊下令独立.然后,如果失败,我使用ZJsonPatch告诉我差异是什么.

So I tried ZJsonPatch. I like the fact that using Jackson with it, the ordering of the members does not matter. In other words, I first try to check for equality using Jackson. Jackson is ordering independent. Then if it fails, I use ZJsonPatch to tell me what the diff is.

{"op":"replace","path":"/data/0/successfulIds/1","value":"B9"}

可以很好地处理嵌套的JSON.

which handles nested JSON well.

ObjectMapper mapper = new ObjectMapper();
JsonNode expected = mapper.readTree(expectedJsonResponse);
JsonNode actual = mapper.readTree(actualJsonResponse);

try {
    assertEquals(expected, actual);
} catch (AssertionError ae) {
    JsonNode patch = JsonDiff.asJson(actual, expected);
    throw new Exception(patch.toString(), ae);
}

这篇关于使用JSONAssert查找json diff失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆