是否可以比较结构不同的两个属性文件的键?

问题描述 投票:1回答:1

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

我尝试使用WinMerge和FileMerge之类的工具,但没有任何运气。

文件1:

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

File2:

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

.
.
.

}

在此示例中,我希望有一个工具o报告,指示在文件1和文件2中找到了CardShipmentScale,但在文件1中仅找到了CheckboxCard

有什么想法吗?

json properties-file
1个回答
0
投票

您可以从此答案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.
© www.soinside.com 2019 - 2024. All rights reserved.