Глубокое сравнение двух JSON и демонстрация различийJAVA

Программисты JAVA общаются здесь
Anonymous
Глубокое сравнение двух JSON и демонстрация различий

Сообщение Anonymous »

Я пытаюсь сравнить два динамических данных JSON, и если они не равны, то я печатаю различия. Для этого я использую < /p>

Код: Выделить всё

Type mapType = new TypeToken(){}.getType();
Map firstMap = g.fromJson(jsonElement1, mapType);
Map secondMap = g.fromJson(jsonElement2, mapType);
System.out.println(Maps.difference(firstMap, secondMap));
< /code>

Мне нужно отобразить разницу в консоли.
Это код Java, в котором я пытаюсь отобразить различия json < /p>

      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;
}
}
< /code>

Массив объектов < /p>

if (isEqual) {
Object[] o1 = ls1.toArray();
Object[] o2 = ls2.toArray();
isEqual = Arrays.deepEquals(o1, o2);
}
< /code>

Массив объектов o1 и o2 - null Не знаю, почему он нулевой.
Это два образца, которые я использую для тестирования json1 {"" Список компаний ": [" Compnay: Paypal "," Compnay: eBay "," Compnay: Google "] < /code> json1 {" "Компания": ":" ":" Компания ":": "Компания": ":" Компания ":": "Компания": ":" Компания ":": "Компания": ":" Компания ":" Компания ":" Компания ":" Компания ":" Компания ":" Компания ":" CompNay: Viswesh "," compnay: anand "," compnay: anand "]} 
в обоих json значение для компании отличается>

Подробнее здесь: https://stackoverflow.com/questions/413 ... ifferences

Вернуться в «JAVA»