在文件中打印时拆分值

时间:2017-01-06 03:46:57

标签: java json file guava

我在文件中打印一个值我需要拆分它们第一个内容完成后留下一些空格然后打印下一个

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方法从两个文件中获取差异并打印它们。我需要首先打印第一个文件差异然后留下一些空格并打印下一个文件差异。我这样做是为了单独显示内容。目前它显示没有任何空间,因此很难识别。

我需要这样的东西 第一个文件中的差值:

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

第二档的差异:

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

方法entriesOnlyOnLeft()和entriesOnlyOnRight()不显示差异,因为键应该不同但这里的键是相同的

1 个答案:

答案 0 :(得分:1)

如果要打印左右两侧分隔的地图差异,可以将它们与MapDifference结果分开:

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

包含价值差异:

bw.write("Value differences: " + diff.entriesDiffering());
相关问题