2017-01-06 2 views
0

내가 그들을 분할해야하는 첫 번째 내용이 약간의 공간을두고 그 다음 하나분할 값 동안 파일에 값을 인쇄하고

public class Test_Json { 


    public static ArrayList<Object> ls1 = new ArrayList<Object>(); 
    public static ArrayList<Object> ls2 = new ArrayList<Object>(); 


    public static void main(String[] args) throws Exception { 
     JsonParser parser = new JsonParser(); 
     BufferedWriter bw = null; 
     FileWriter fw = null; 

     try { 
      Gson g = new Gson(); 
      JsonElement jsonElement1 = parser.parse(new FileReader("D://sample1.json")); 
      JsonElement jsonElement2 = parser.parse(new FileReader("D://sample2.json")); 
      // System.out.println("Is the two JSON File Same: "+compareJson(jsonElement1,jsonElement2)); 
      if (!compareJson(jsonElement1, jsonElement2)) { 
       Type mapType = new TypeToken<Map<String, Object>>() { 
       }.getType(); 
       Map<String, Object> firstMap = g 
         .fromJson(jsonElement1, mapType); 
       Map<String, Object> secondMap = g.fromJson(jsonElement2, 
         mapType); 
       System.out.println(" The Two JSON Files Are Not the Same "); 
       System.out.println(Maps.difference(firstMap, secondMap)); 
       String s = Maps.difference(firstMap, secondMap).toString(); 
       try{ 
        fw = new FileWriter("D:\\output.txt"); 
        bw = new BufferedWriter(fw); 
        bw.write(s); 
       }catch(Exception e){ 
        e.printStackTrace(); 
       } 

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


     if (json1 != null && json2 != null) { 


      if (json1.isJsonObject() && json2.isJsonObject()) { 
       Set<Entry<String, JsonElement>> ens1 = ((JsonObject) json1) 
         .entrySet(); 
       Set<Entry<String, JsonElement>> ens2 = ((JsonObject) json2) 
         .entrySet(); 
       JsonObject json2obj = (JsonObject) json2; 
       if (ens1 != null && ens2 != null 
         && (ens2.size() == ens1.size())) { 

        for (Entry<String, JsonElement> en : ens1) { 
         isEqual = isEqual 
           && compareJson(en.getValue(), 
             json2obj.get(en.getKey())); 
        } 
       } else { 
        return false; 
       } 
      } 

      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; 

        for (JsonElement je : jarr1) { 
         isEqual = isEqual && compareJson(je, jarr2.get(i)); 
         i++; 
        } 
        if (isEqual) { 
         ls1.toArray(); 
         ls2.toArray(); 
         isEqual = ls1.containsAll(ls2); 
        } 
       } 
      } 

      else if (json1.isJsonNull() && json2.isJsonNull()) { 
       return true; 
      } 

      else if (json1.isJsonPrimitive() && json2.isJsonPrimitive()) { 

       ls1.add(json1); 
       ls2.add(json2); 
       return true; 
      } 
      else if((json1.isJsonPrimitive() & json2.isJsonArray()) || (json2.isJsonPrimitive() && json1.isJsonArray())){ 
       return false; 
      } 
     } else if (json1 == null && json2 == null) { 
      return true; 
     } else { 
      return false; 
     } 

     return isEqual; 
    } 
} 

Map.difference 방법은 얻을 인쇄 완료 onces 차이가 두 파일을 형성하고 인쇄합니다. 먼저 첫 번째 파일 차이를 인쇄 한 다음 몇 줄의 공백을 남겨두고 다음 파일 차이를 인쇄해야합니다. 이것은 콘텐츠를 별도로 표시하기 위해 수행 중입니다. 현재 그것은 공백없이 표시되므로 식별하기가 어렵습니다. 제 파일

{ 
    "id": 1, 
    "name": "A green door", 
    "price": 12.50, 
    "tags": ["home", "green"] 
} 

차이 :

는 I 먼저 파일이 차이 값과 같은 필요

{ 
    "id": 14, 
    "name": "A green door bell", 
    "price": 127.50, 
    "tags": ["home", "green"] 
} 

있어서 entriesOnlyOnLeft() 및 entriesOnlyOnRight()를 differece로서 표시되지 키가 달라야하지만 여기서 키가 동일해야합니다.

+1

문제는 무엇인가 :

MapDifference diff = Maps.difference(firstMap, secondMap); bw.write("Only on left: " + diff.entriesOnlyOnLeft()); // add separation bw.write("Only on right: " + diff.entriesOnlyOnRight()); 

값의 차이를 포함하려면? – shmosel

+0

@shmosel 일단 내가 가지고있는 두 개의 json 파일 사이에 발견 된 차이점은 일종의 분리가되어야하므로 첫 번째 값과 두 번째 값을 식별 할 수 있습니다. 현재 차이가 파일 전체에 인쇄됩니다. –

+0

왼쪽 및 오른쪽 diff를 분리 하시겠습니까? – shmosel

답변

1

왼쪽과 오른쪽 사이의 간격으로 맵 차이를 인쇄하려면 우측, 당신은 MapDifference 결과에서 별도로 얻을 수 있습니다 :

bw.write("Value differences: " + diff.entriesDiffering()); 
+0

위 코드를 시도했지만 파일이 비어 있습니다. 즉, 파일에 차이점이 없습니다. –

+0

먼저 파일을 열어야합니다. – shmosel

+0

MapDifference에는 diff.entriesOnlyOnLeft() 값이 있지만 diff.entriesOnlyOnRight()는 빈 문자열을 출력합니다 –