How would you suggest this task is approached?
The challenge as i see it is in presenting diff information intelligently. Before i go reinventing the wheel, is there
This only addresses equality, not differences.
With Jackson.
ObjectMapper mapper = new ObjectMapper();
JsonNode tree1 = mapper.readTree(jsonInput1);
JsonNode tree2 = mapper.readTree(jsonInput2);
boolean areTheyEqual = tree1.equals(tree2);
From the JavaDoc for JsonNode.equals:
Equality for node objects is defined as full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing equality of root nodes.
In case if want to compare two JSON Ignoring the order of elements/object, see if this library helps you.
Usage:
List<Diff> diff = new DnjsonDiff().getDiff(String left, String right)
Left =[{"name":"Bob","businessKey":"4444"},{"name":"John","businessKey":"5555"}]
Right = [{"name":"Bob","businessKey":"6666"},{"name":"John", "businessKey":"4444"}]
Diff:
[
{
"op": "replace",
"configuredValue": "John",
"intendedValue": "Bob",
"businessKey": "4444"
},
{
"op": "remove",
"configuredValue": null,
"intendedValue": "{\"name\":\"John\",\"businessKey\":\"5555\"}",
"businessKey": "5555"
},
{
"op": "add",
"configuredValue": "{\"name\":\"Bob\",\"businessKey\":\"6666\"}",
"intendedValue": null,
"businessKey": "null"
}
]
I've done good experience with JSONAssert.
import org.junit.Test;
import org.apache.commons.io.FileUtils;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
...
@Test
public void myTest() {
String expectedJson = FileUtils.readFileToString("/expectedFile");
String actualJson = FileUtils.readFileToString("/actualFile");
JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.STRICT);
}
...
Sr. this is my solution for java code, over my app;
try {
// Getting The Array "Courses" from json1 & json2
Courses1 =json1.getJSONArray(TAG_COURSES1);
Courses2 = json2.getJSONArray(TAG_COURSES);
//LOOP FOR JSON1
for(int i = 0; i < Courses1.length(); i++){
//LOOP FOR JSON2
for(int ii = 0; ii < Courses2.length(); ii++){
JSONObject courses1 = Courses1.getJSONObject(i);
JSONObject courses2 = Courses2.getJSONObject(ii);
// Storing each json1 item in variable
int courseID1 = courses1.getInt(TAG_COURSEID1);
Log.e("COURSEID2:", Integer.toString(courseID1));
String Rating1 = courses1.getString(TAG_RATING1);
int Status1 = courses1.getInt(TAG_STATUS1);
Log.e("Status1:", Integer.toString(Status1)); //Put the actual value for Status1 in log.
// Storing each json2 item in variable
int courseID2 = courses2.getInt(TAG_COURSEID);
Log.e("COURSEID2:", Integer.toString(courseID)); //Put the actual value for CourseID in log
String Title2 = courses2.getString(TAG_TITLE);
String instructor2 = courses2.getString(TAG_INSTRUCTOR);
String length2 = courses2.getString(TAG_LENGTH);
String rating2 = courses2.getString(TAG_RATING);
String subject2 = courses2.getString(TAG_SUBJECT);
String description2 = courses2.getString(TAG_DESCRIPTION);
//Status1 = 5 from json1; Incomplete, Status1 =-1 Complete
if(Status1 == 5 && courseID2 == courseID1){
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
//Storing the elements if condition is true.
map.put(TAG_COURSEID, Integer.toString(courseID2)); //pend for compare
map.put(TAG_TITLE, Title2);
map.put(TAG_INSTRUCTOR, instructor2);
map.put(TAG_LENGTH, length2);
map.put(TAG_RATING, rating2);
map.put(TAG_SUBJECT, subject2); //show it
map.put(TAG_DESCRIPTION, description2);
//adding HashList to ArrayList
contactList.add(map);
}//if
}//for2 (json2)
} //for1 (json1)
}//Try
Hope this help others, of course, Just put your values and conditions, and the kind of view, in this case; Hashmap over a listview.