Comparing JSON Objects in Java without Regard to Child Order
When unit testing JSON responses from web services, parsing libraries that compare JSON objects while disregarding child order can be invaluable.
Solution: Skyscreamer's JSONAssert
Skyscreamer's JSONAssert library provides a solution. Its "non-strict" mode allows:
In strict mode, JSONAssert functions similarly to json-lib's test class.
Example Usage
<code class="java">@Test public void testGetFriends() { JSONObject data = getRESTData("/friends/367.json"); String expected = "{friends:[{id:123,name:\"Corby Page\"}" + ",{id:456,name:\"Solomon Duskis\"}]}"; JSONAssert.assertEquals(expected, data, false); // "false" for non-strict mode }</code>
Additional Features
The above is the detailed content of How to Compare JSON Objects without Regarding Child Order in Java?. For more information, please follow other related articles on the PHP Chinese website!