我想比较两个JSON字符串,这是一个巨大的层次结构,并想知道它们在值上的不同之处.但是某些值是在运行时生成的并且是动态的.我想从我的比较中忽略那些特定的节点.
我目前正在使用org.SkyScreamer的JSONAssert进行比较.它给了我很好的控制台输出,但不会忽略任何属性.
对于前
java.lang.AssertionError messageHeader.sentTime
expected:null
got:09082016 18:49:41.123
Run Code Online (Sandbox Code Playgroud)
现在这是动态的,应该被忽略.就像是
JSONAssert.assertEquals(expectedJSONString, actualJSONString,JSONCompareMode, *list of attributes to be ignored*)
Run Code Online (Sandbox Code Playgroud)
如果有人在JSONAssert中建议解决方案,那将会很棒.然而,其他方式也是受欢迎的.
我需要比较两个表示json对象的字符串。为了进行测试,我需要一种比较这些字符串的方法,不仅要忽略子元素的顺序(这很常见),而且要忽略jsons数组属性中的元素顺序。即:
group: {
id: 123,
users: [
{id: 234, name: John},
{id: 345, name: Mike}
]
}
Run Code Online (Sandbox Code Playgroud)
应该等于:
group: {
id: 123,
users: [
{id: 345, name: Mike},
{id: 234, name: John}
]
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我需要一些javascript lib,但是也欢迎使用其他方法。
我对 Java 比较陌生,我要求编写 JSON 响应服务器的测试。我发现JSONassert非常有用,但是我没有成功编写该方法getRESTData。
有人可以帮忙吗?
@Test
public void testGetFriends() throws JSONException {
JSONObject data = getRESTData("/friends/367.json");
String expected = "{friends:[{id:123,name:\"Corby Page\"}"
+ ",{id:456,name:\"Solomon Duskis\"}]}";
JSONAssert.assertEquals(expected, data, false);
}
Run Code Online (Sandbox Code Playgroud) 我在一个使用 REST 接口的开源项目中工作。为了验证(将实际响应与预期匹配)我们在 JUnit 中的其余接口,我们想使用 JSONAssert。(https://github.com/skyscreamer/JSONassert)。但是我的用法有问题。请帮助解决它。
Expected JSON:
{
"objectId": "[^\\s]",
"protocol": "[^\\s]",
"hostName": "[^\\s]",
"port": "[^\\s]",
"commParams": "[^\\s]"
}
Run Code Online (Sandbox Code Playgroud)
备注:objectId/protocol/hostName/port/commParams 可以是任何值但不能为空
Actual JSON:
{
"objectId": "controller2",
"protocol": "ftp",
"hostName": "sdnorchestrator",
"port": "21",
"commParams": "username:tomcat, password:tomdog"
}
Run Code Online (Sandbox Code Playgroud)
问题1:JSON Assert的哪个接口,我需要使用哪个接口来解决上面的问题:下面一个?
JSONAssert.assertEquals("Expected JSON", "Actual JSON" new CustomComparator(
JSONCompareMode.LENIENT_ORDER, new Customization(PREFIX, new RegularExpressionValueMatcher<Object>())));
Run Code Online (Sandbox Code Playgroud)
问题 2:这里的 PREFIX 应该是什么?(我尝试使用“ ”、“.”、“ . ”但没有成功)
也欢迎针对上述问题提出任何其他建议(JSONAssert 除外)。
我想知道是否有任何 JSONAssert 库的替代方案允许以类似的方式断言 JSON 文件。具有与 JSONCompareMode 类似功能的替代方案,如 STRICT 和 NON_EXTENSIBLE 等。 JSONAssert 中的最后一次提交似乎很旧 -> https://github.com/skyscreamer/JSONassert/commits/master看起来该项目不是发展了。
我想比较Java 8中的两个JSON字符串是否相等,但忽略特定的已知节点,这些节点包含的值应该不同并且可以忍受,例如时间戳。当前使用的是来自org.SkyScreamer的JSONAssert v1.5.0,我能够“忽略”许多这样的节点,但是不能“忽略”数组中的节点。
我想要扩展当前的JSONComparator,使其包含一个Customization,该Customization的第一个“ path”参数具有数组地址。
JSONComparator customisedJobComparator = new CustomComparator(JSONCompareMode.NON_EXTENSIBLE,
new Customization("id", (o1, o2) -> true),
new Customization("appointmentBookedDate.$date", (o1, o2) -> true),
...
new Customization("someArray[n].timestamp", (o1, o2) -> true) //<--- this is wrong, what is the correct way for this path address?
);
Run Code Online (Sandbox Code Playgroud)
下面的代码是我尝试证明解决方案的尝试;除了anArray []。id值外,两个预期/实际JSON字符串都相同。我希望此测试通过,但失败并出现错误:java.lang.AssertionError:anArray [0]找不到元素{“ id”:“ valueA”}的匹配项
@Test
public void compareJsonStrIgnoringDiffInArray() {
String errorMsg = "";
String expectedJsonStr = "{\"anArray\": [{\"id\": \"valueA\"}, {\"colour\": \"Blue\"}]}";
String actualJsonStr = "{\"anArray\": [{\"id\": \"valueB\"}, {\"colour\": \"Blue\"}]}";
//Create custom comparator which compares two json …Run Code Online (Sandbox Code Playgroud) 我有以下 JSON 结构
{
"name": "xyz",
"address": {
"street": "avenida",
"number": "41414-44141",
"code": "33ll",
"moreFields": "some data"
},
"moreFields": "some data"
}
Run Code Online (Sandbox Code Playgroud)
在我的 JUNIT 类中,我必须比较具有上述结构的两个 JSON 文件。但是我想忽略字段address.number和address.code。我知道我可以使用下面的代码来忽略一个字段,但是我如何更改它以适应我的要求?
assertEquals(json1, json2,
return new CustomComparator(JSONCompareMode.NON_EXTENSIBLE,
Customization.customization("address.code",
(o1, o2) -> {
return true;
})
));
Run Code Online (Sandbox Code Playgroud)
查看实现,我们为自定义方法提供的正则表达式似乎已被修改,我无法得出可以作为条件path的参数值OR。
非常感谢任何建议
谢谢!
我们最近更新了REST集成测试的依赖项,该测试使用了Groovy,HTTPBuilder,JSONAssert和Junit.当我们从HTTPBuilder 0.5.2到0.6时,我们的许多测试都失败了.
我们发现由于HTTPBuilder中的一个新"功能"提供了"对已注册内容类型的自动响应解析",响应格式发生了变化.
旧的,(0.5.2)格式,预期响应:
[
{
"name":"Portfolio_REST_Test01",
"description":"",
"referenceValueType":"Net Value",
"unitType":"SHARES",
"tags":[]
}
]
新的(0.6.2)响应格式:
[{tags=[], referenceValueType=Net Value, unitType=SHARES, description=, name=Portfolio_REST_Test01}]
Run Code Online (Sandbox Code Playgroud)
当JSONAssert尝试解析值为空字符串的命名值时,会出现问题,请参阅上面示例中的"描述".JSONAssert期望一个字符遵循等号而不是逗号,并在遇到时抛出异常.
json ×8
jsonassert ×8
java ×6
junit ×2
compare ×1
equals ×1
groovy ×1
httpbuilder ×1
javascript ×1