比较Java中的两个.json文件?

时间:2018-04-12 06:17:28

标签: java json collections

首先,我是处理JSON的新手。现在我将使用java比较两个.json文件。如果有人熟悉这个,请你指导我最好的方法。提前谢谢。

我的json文件如下所示。

file1.json
[{
    "GEO_ENERGY_RESOURCES_LIMITED": {
      "Total Assets Consolidated": "309,074,176",
      "Total Assets Company": "226,919,588",
      "Date": "31-Dec-16",
      "Currency": "US Dollar",
      "Total Revenue Consolidated": "182,108,648",
      "Total Revenue Company": "Not Found",
      "RDate": "31-Dec-16"
    }
  },
  {
    "GLOBAL_LOGISTIC_PROPERTIES_LIMITED": {
      "Total Assets Consolidated": "21,759,755,000",
      "Total Assets Company": "9,233,746,000",
      "Date": "31-Mar-17",
      "Currency": "US Dollar",
      "Total Revenue Consolidated": "886820000",
      "Total Revenue Company": "Not Found",
      "RDate": "31-Mar-17"
    }
  }]

file2.json
[{
    "GEO_ENERGY_RESOURCES_LIMITED": {
      "Total Assets Consolidated": "",
      "Total Assets Company": "226,919,588",
      "Date": "01-Dec-16",
      "Currency": "US Dollar",
      "Total Revenue Consolidated": "182,108,648",
      "Total Revenue Company": "Not Found",
      "RDate": "31-Dec-16"
    }
  },
  {
    "GLOBAL_LOGISTIC_PROPERTIES_LIMITED": {
      "Total Assets Consolidated": "21,759,755,000",
      "Total Assets Company": "9,233,746,000",
      "Date": "31-Mar-17",
      "Currency": "US Dollar",
      "Total Revenue Consolidated": "",
      "Total Revenue Company": "Not Found",
      "RDate": "31-Mar-17"
    }
  }]

1 个答案:

答案 0 :(得分:1)

您需要Jackson将json转换为模型类。这是example

首先,创建模型:

@JsonRootName(value = "user")
public class Person {
    @JsonProperty("name")
    private String username;
    private int age;
    // getter and setter
}

然后将json转换为object:

ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'bejond', 'age': 18}";
Person person = mapper.readValue(jsonInString, Person.class);

现在你得到了人,你可以获得价值并相互比较。您可以使用@JsonPropery和其他注释将属性映射到json属性,即使它们的名称不同。