I have a JSONArray of the form :
[[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}], [{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}], [{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}]]
I want to compare individual fields like in the above case
since { "Country" : "IN", "count" : 10}
is equal to { "Country" : "IN", "count" : 10}
and { "Country" : "IN", "count" : 10}
and { "Country" : "US", "count" : 20}
is equal to { "Country" : "US", "count" : 20}
and { "Country" : "US", "count" : 20}
, i Should get a Match result.
However for a case like below, I should get a unmatch result as the count
don't match.
[[{ "Country" : "IN", "count" : 45},{ "Country" : "US", "count" : 60}],
[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}],
[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}]]
I was able to put the data into a HashMap. But I am unable to find a way, how to compare.
myArray
contains the above 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;
Thanks,