【问题标题】:Is there a way to compare keys of two property files with different structure?有没有办法比较具有不同结构的两个属性文件的键?
【发布时间】:2019-11-02 12:41:53
【问题描述】:

我有两个结构不同的 +10.000 行属性文件,我需要知道两个文件中的键。

我尝试使用 WinMerge 和 FileMerge 等工具,但没有成功。

文件1:

.
.
.
  "CardShipmentScale.shipmentScaleRegional": "SomeText1",
  "CardShipmentScale.shipmentScaleRegionalExplanation": "SomeText2",
  "CheckboxCard.DAY": "SomeText3",
  "CheckboxCard.MONTH": "SomeText4",
  "SomeOtherKey.SomeOtherSubKey": "SomeOtherText5"
.
.
.

文件2:

{ "global" : {
.
.
.
      CardShipmentScale : {
          shipmentScaleRegional : "SomeText1",
          shipmentScaleRegionalExplanation : "SomeText2"
                          },
      SomeOtherKey : {
          SomeOtherSubKey : "SomeOtherText5"
                     },

.
.
.

}

在本例中,我想要一个工具 o 报告,表明在 file1 和 file2 中找到了 CardShipmentScale,但仅在 file1 中找到了 CheckboxCard

有什么想法吗?

【问题讨论】:

  • File1File2的格式是什么? File1 看起来不像一个有效的属性文件,File2 看起来像一个有效的 JSON 文件。 global 节点是否包含所有子节点并且它是文件中的根?您想使用哪种语言?
  • Michal 感谢您的评论,File1 和 File2 都是有效的 Json 文件,在 File2 全局中包含所有子节点。
  • 关于语言,我对任何一个都很满意

标签: json properties-file


【解决方案1】:

你可以从这个答案Flattening a 3 level nested JSON string in java开始。我在那里创建了JsonFlattener,它将JSON 文件转换为Map,其中键是JSON Path

现在应该很容易检查第二个文件中第一个文件中的每个键:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

public class JsonPathApp {

    public static void main(String[] args) throws Exception {
        File jsonFile0 = new File("./resource/test.json").getAbsoluteFile();
        File jsonFile1 = new File("./resource/test1.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        Map<String, JsonNode> map0 = new JsonFlattener(mapper.readTree(jsonFile0)).flatten();
        Map<String, JsonNode> map1 = new JsonFlattener(mapper.readTree(jsonFile1)).flatten();

        for (Map.Entry<String, JsonNode> node : map0.entrySet()) {
            String keyToCheck = "/global" + node.getKey().replace('.', '/');
            if (map1.containsKey(keyToCheck)) {
                System.out.println(node.getKey() + " exists in both files.");
            } else {
                System.out.println(node.getKey() + " exists only in 1 file.");
            }
        }
    }
}

test.json 包含:

{
  "CardShipmentScale.shipmentScaleRegional": "SomeText1",
  "CardShipmentScale.shipmentScaleRegionalExplanation": "SomeText2",
  "CheckboxCard.DAY": "SomeText3",
  "CheckboxCard.MONTH": "SomeText4",
  "SomeOtherKey.SomeOtherSubKey": "SomeOtherText5"
}

test1.json 包含:

{
  "global": {
    "CardShipmentScale": {
      "shipmentScaleRegional": "SomeText1",
      "shipmentScaleRegionalExplanation": "SomeText2"
    },
    "SomeOtherKey": {
      "SomeOtherSubKey": "SomeOtherText5"
    }
  }
}

对于上述文件应用程序打印:

/CardShipmentScale.shipmentScaleRegional exists in both files.
/CardShipmentScale.shipmentScaleRegionalExplanation exists in both files.
/CheckboxCard.DAY exists only in 1 file.
/CheckboxCard.MONTH exists only in 1 file.
/SomeOtherKey.SomeOtherSubKey exists in both files.

【讨论】:

  • 这行得通,我只是更改了两行: Map map0 = new JsonFlatener(mapper.readTree(jsonFile0).toString()).flattenAsMap(); Map map1 = new JsonFlatener(mapper.readTree(jsonFile1).toString()).flattenAsMap();非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 2014-11-20
  • 2016-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
相关资源
最近更新 更多