java - 在文件中打印时分割值

标签 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()不显示差异,因为 key 应该不同,但这里的 key 是相同的

最佳答案

如果你想打印左右分隔的 map 差异,可以从 MapDifference 中单独获取。结果:

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

要包含值(value)差异:

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

关于java - 在文件中打印时分割值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41498618/

相关文章:

Java无法创建新进程: too many open file error

c - 通过c中的函数传递文件

Java-递归-矩阵修改

java - snappy 可以和 Redis 一起使用吗

android - 尝试从 URL : Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname 解析 JSON 数据

javascript - Laravel 5.5 - 从 JSON 对象数组为数据表创建列标题

java - 使用 Java 将文本文件读入数组会生成异常

java - 你认为我滥用了静力学吗?

java - 使用版本控制很好地重构

python - Twitter api python - sys.argv 和 json