Java JSONArray中的多个匹配

Java JSONArray中的多个匹配,java,json,hashmap,Java,Json,Hashmap,我有一个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}]] 我想比较像上面这种情况的各

我有一个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}]]
我想比较像上面这种情况的各个字段 由于
{“国家”:“IN”,“count”:10}
等于
{“国家”:“IN”,“count”:10}
{“国家”:“IN”,“count”:10}
{“国家”:“US”,“count”:20}
{“国家”:“US”,“count”:20}和
{“国家”:“US”,“count”:20}
,我应该得到一个匹配的结果

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

[[{ "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;
int length=myArray.getJSONArray(0.length();
Map cargo=newhashmap();
对于(int j=0;j

谢谢,

您可以创建一个名为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
 }
}
看看如何实现
equals
hashcode
方法


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

创建一个包含两个变量的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
              }

            }
Country=null;
对于(int j=0;j

一旦你有了POJO的列表,你就可以做同样的事情,包含和所有其他奇特的操作以了解匹配情况。

您将希望将JSON数组转换为java对象,并覆盖equals和hashcode方法。@alayor是否有一些链接/代码可以让我指向Overide equals和hashcode方法?@Aya:alayor的评论中有四个明显的关键字。在Google(或Stack Overflow)的搜索字段中键入“Java”、“override”、“equals”和“hashCode”将比要求我们为您执行搜索更简单、更快。@KevinJ.Chase我知道您是对的,但您没有义务回复。这完全取决于你。对不起,如果我听起来有点刺耳的话。我正在转换
cargo
以获取像这样的国家/地区对象
Map cargo=new HashMap();最终ObjectMapper映射器=新ObjectMapper();最终国家pojo=mapper.convertValue(货物、国家/地区类别)
然后调用as
cargo.putAll((Map
Map cargo=new HashMap();
。您可能只想使用List。
List cargo=new ArrayList()
谢谢。现在,我可以在不使用jackson的情况下转换它。一旦我在hashmap
cargo
中有了它,我应该迭代类似的键,检查它们是否具有相同的匹配值。对吗?我只是在想,在包含POJO对象的hashmap创建的原始方法中,我应该返回什么。我到底需要实现什么emt
hashcode
method?。我的意思是我可以使用equals方法,因为我只关心在输入国家地图后比较值。请看以下答案: