Java 如何在两个JSON文件中查找差异并打印出现差异的行号
我有两个JSON文件,对象的顺序相同。我想打印两者的差异,并打印出现差异的行号。 例如 输出应如下所示: “在Expected.json中发现差异: 在“此处打印行号”-->此处打印差异。类似以下内容:Java 如何在两个JSON文件中查找差异并打印出现差异的行号,java,json,gson,compare,Java,Json,Gson,Compare,我有两个JSON文件,对象的顺序相同。我想打印两者的差异,并打印出现差异的行号。 例如 输出应如下所示: “在Expected.json中发现差异: 在“此处打印行号”-->此处打印差异。类似以下内容: Scanner file1 = new Scanner(new File(file1Path)); Scanner file2 = new Scanner(new File(file2Path)); int lineCounter = 1; while(file1.hasNextLine()
Scanner file1 = new Scanner(new File(file1Path));
Scanner file2 = new Scanner(new File(file2Path));
int lineCounter = 1;
while(file1.hasNextLine() && file2.hasNextLine()) {
lineFile1 = file1.nextLine();
lineFile2 = file2.nextLine();
if(!lineFile1.equals(lineFile2)) {
System.out.println("Difference found in Expected.json: on " + lineCounter);
System.out.println("File1 line: " + lineFile1);
System.out.println("File2 line: " + lineFile2);
}
lineCounter++;
}