【问题标题】:Multiple matching within a JSONArrayJSONArray 中的多重匹配
【发布时间】:2017-09-06 20:07:41
【问题描述】:

我有一个 JSONArray 格式:

    [[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}], [{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}], [{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}]]

我想比较上述案例中的各个字段 因为{ "Country" : "IN", "count" : 10} 等于{ "Country" : "IN", "count" : 10}{ "Country" : "IN", "count" : 10}{ "Country" : "US", "count" : 20} 等于{ "Country" : "US", "count" : 20}{ "Country" : "US", "count" : 20},我应该得到一个匹配结果。

但是对于下面这样的情况,我应该得到一个不匹配的结果,因为 count 不匹配。

[[{ "Country" : "IN", "count" : 45},{ "Country" : "US", "count" : 60}],
[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}],
[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}]]

我能够将数据放入 HashMap。但是我找不到方法,如何比较。

myArray 包含上述 JSONArray。

int length = myArray.getJSONArray(0).length();
Map<String, Integer> cargo = new HashMap<>();
for (int j = 0; j < myArray.length(); j++) {
  for (int k = 0; k < myArray.getJSONArray(j).length(); k++) {
    String country = myArray.getJSONArray(j).getJSONObject(k).getString(DataConstants.COUNTRY);
    Integer count = myArray.getJSONArray(j).getJSONObject(k).getInt(DataConstants.COUNT);
    cargo.put(country, count);
  }

}

if (cargo.size() == length) {
  System.out.println("Data Matched !!");
  return true;
}

else
  System.out.println("Data Not Matched !!");
return false;

谢谢,

【问题讨论】:

  • 您需要将 JSON 数组转换为 java 对象并覆盖 equals 和 hashcode 方法。
  • @alayor 有没有一些链接/代码你可以指点我 overeeride equals 和 hashcode 方法?
  • @Aya:在 alayor 的评论中有四个明显的关键词。在 Google(或 Stack Overflow)的搜索字段中输入“Java”、“override”、“equals”和“hashCode”会比要求我们为您做更容易和更快。
  • @KevinJ.Chase 我知道你是对的,但你没有义务回应。这完全取决于你。对不起,如果我听起来很苛刻。

标签: java json hashmap


【解决方案1】:

您可以创建一个名为 Country 的类,您可以使用它来保存 JSON 数组中的数据。

class Country {
 private String countryCode;
 private int count;

 @Override
 public boolean equals(Object obj) {
   // compare your properties
 }

 @Override
 public int hashCode() {
   // Calculate a int using properties
 }
}

查看this tutorial,了解如何实现equalshashcode 方法。

然后,您需要将 JSON 数组转换为 java 对象。看看this tutorial

【讨论】:

  • 为什么我需要实现hashcode 方法?我的意思是我可以只使用 equals 方法,因为我只关心在放入国家地图后比较值。
【解决方案2】:

创建一个包含两个变量的 Pojo 对象并覆盖 equals 方法。

公共类国家{

private String code;
private int count;
//getters, setters

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((code == null) ? 0 : code.hashCode());
    result = prime * result + count;
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Country other = (Country) obj;
    if (code == null) {
        if (other.code != null)
            return false;
    } else if (!code.equals(other.code))
        return false;
    if (count != other.count)
        return false;
    return true;
}

}

现在您可以通过更新代码来创建 POJO 对象的 HashMap。

Country country = null;
        for (int j = 0; j < myArray.length(); j++) {
              for (int k = 0; k < myArray.getJSONArray(j).length(); k++) {
                String country = myArray.getJSONArray(j).getJSONObject(k).getString(DataConstants.COUNTRY);
                Integer count = myArray.getJSONArray(j).getJSONObject(k).getInt(DataConstants.COUNT);
                country = new Country();
                country.setCode(country);
                country.setCount(count);
                cargo.put(country); //change cargo to take country objects
              }

            }

一旦你有了 POJO 的列表,你就可以做等于、包含和所有其他花哨的操作来知道匹配。

【讨论】:

  • 我将 cargo 转换为像 Map&lt;String,Integer&gt; cargo = new HashMap&lt;&gt;(); final ObjectMapper mapper = new ObjectMapper(); final Country pojo = mapper.convertValue(cargo, Country.class); 这样的国家对象,然后调用 cargo.putAll((Map&lt;? extends String, ? extends Integer&gt;) country);。这是正确的吗?
  • Map&lt;Integer, Country&gt; cargo = new HashMap&lt;Integer, Country&gt;();。你可能只想和 List 一起去。 List&lt;Country&gt; cargo = new ArrayList&lt;Country&gt;();
  • 谢谢。现在,我可以在不使用杰克逊的情况下进行转换。一旦我在 hashmap cargo 中有它。我应该只遍历相似的键并检查它们是否具有相同的匹配值。对吧?
  • 我只是在想我应该在包含创建 POJO 对象的 HashMap 的原始方法中返回什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 2022-08-13
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
相关资源
最近更新 更多