Код: Выделить всё
Type mapType = new TypeToken(){}.getType();
Map firstMap = g.fromJson(jsonElement1, mapType);
Map secondMap = g.fromJson(jsonElement2, mapType);
System.out.println(Maps.difference(firstMap, secondMap));
Это Java-код, в котором я пытаюсь отобразить различия JSON
Код: Выделить всё
public class Tester {
public static ArrayList ls1 = new ArrayList();
public static ArrayList ls2 = new ArrayList();
public static void main(String[] args) throws Exception {
JsonParser parser = new JsonParser();
try{
Gson g = new Gson();
JsonElement jsonElement1 = parser
.parse(new FileReader("D:\\file1.json"));
JsonElement jsonElement2 = parser
.parse(new FileReader("D:\\file2.json"));
System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1, jsonElement2));
if(!compareJson(jsonElement1, jsonElement2)){
Type mapType = new TypeToken(){}.getType();
Map firstMap = g.fromJson(jsonElement1, mapType);
Map secondMap = g.fromJson(jsonElement2, mapType);
System.out.println(Maps.difference(firstMap, secondMap));
}
else{
System.out.println("The Two JSON Are SAME!!!!!!!!!!!!!!!");
}
}catch(Exception e1){
e1.printStackTrace();
}
}
public static boolean compareJson(JsonElement json1, JsonElement json2) {
boolean isEqual = true;
// Check whether both jsonElement are not null
if (json1 != null && json2 != null) {
// Check whether both jsonElement are objects
if (json1.isJsonObject() && json2.isJsonObject()) {
Set ens1 = ((JsonObject) json1).entrySet();
Set ens2 = ((JsonObject) json2).entrySet();
JsonObject json2obj = (JsonObject) json2;
if (ens1 != null && ens2 != null && (ens2.size() == ens1.size())) {
// Iterate JSON Elements with Key values
for (Entry en : ens1) {
isEqual = isEqual && compareJson(en.getValue(),json2obj.get(en.getKey()));
}
} else {
return false;
}
}
// Check whether both jsonElement are arrays
else if (json1.isJsonArray() && json2.isJsonArray()) {
JsonArray jarr1 = json1.getAsJsonArray();
JsonArray jarr2 = json2.getAsJsonArray();
if (jarr1.size() != jarr2.size()) {
return false;
} else {
int i = 0;
// Iterate JSON Array to JSON Elements
for (JsonElement je : jarr1) {
isEqual = isEqual && compareJson(je, jarr2.get(i));
i++;
}
if (isEqual) {
Object[] o1 = ls1.toArray();
Object[] o2 = ls2.toArray();
isEqual = Arrays.deepEquals(o1, o2);
}
}
}
// Check whether both jsonElement are null
else if (json1.isJsonNull() && json2.isJsonNull()) {
return true;
}
// Check whether both jsonElement are primitives
else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) {
ls1.add(json1);
ls2.add(json2);
}
} else if (json1 == null && json2 == null) {
return true;
} else {
return false;
}
return isEqual;
}
}
Код: Выделить всё
if (isEqual) {
Object[] o1 = ls1.toArray();
Object[] o2 = ls2.toArray();
isEqual = Arrays.deepEquals(o1, o2);
}
Это два примера json, которые я использую для тестирования json1 {"Company List":["Compnay: Paypal","Compnay: eBay","Compnay: Google"] json2 {"Company List":["Compnay: Viswesh","Compnay: Anand","Compnay: Anand"] в обоих json значения компании разные, поэтому код должен возвращать false, но код возвращает true
Мобильная версия