0

I`m trying to get the diferences between two jsonArrays, in different files, and print it in a new file. What is the best practice for this? I hope you can help me. Thanks!

I`m using eclipse. I´ve tried with Maps.difference reading the files with a fileReader.

enter code here   ://Reading the file
 File jsonInputFileMod = new File("../MENU.json");
        InputStream isMod;
        is = new FileInputStream(jsonInputFileMod);
        // Create JsonReader from Json.
        JsonReader readerMod = Json.createReader(is);
        // Get the JsonObject structure from JsonReader.
        JsonArray empObjMod = readerMod.readArray();
        readerMod.close(); 
//Creating maps
 Map [] mapArray = new  Map [empObj.size()];
        for(int i=0; i<empObj.size(); i++){

            mapArray[i] = (Map) empObj.get(i);

        }

        Map [] mapArrayMod = new  Map [empObjMod.size()];
        for(int i=0; i<empObjMod.size(); i++){

            mapArrayMod[i] = (Map) empObjMod.get(i);
//Comparation
if(mapArray.length==mapArrayMod.length){
            String [] dif = new String [mapArray.length];
            FileWriter salida = new FileWriter("../diferences.json");
            for(int i=0; i<mapArray.length; i++){

            dif[i] = Maps.difference(mapArray[i], mapArrayMod[i]).toString();

            salida.write("\n\n JSON : " + i + "\n\n");

            //salida.write(Maps.difference(mapArray[i], mapArrayMod[i]).toString().replace("[", "\n\t["));
            salida.write(dif[i]);

            }
            salida.close();
  • I see it in this way, 1. Read JSON and convert it to Objects. 2. When you convert the objects use collections framework methods to retain only those objects which are not same. 3. Write the objects (which are not same) to the different file. 4. You need to implement hashCode and equals in your object for comparison. – Bilbo Baggins Jul 12 '19 at 10:04
  • try here: https://stackoverflow.com/questions/6526911/best-way-to-compare-2-json-files-in-java – diginoise Jul 12 '19 at 10:17

0 Answers0