如何在两个JSON文件中查找差异并打印发生差异的行号

我有两个JSON文件,对象顺序相同。我想同时打印两者中的差异,并打印发生差异的行号。 例如。 输出应为:

”在Expected.json中发现的差异:  在“在此处打印LineNumber”上->在此处打印差异。

mingdou 回答:如何在两个JSON文件中查找差异并打印发生差异的行号

类似这样的东西:

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++;
}
本文链接:https://www.f2er.com/3087246.html

大家都在问