gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Java code to compare 2 jsons using recurssion
- User
Java code to compare 2 jsons using recurssion
- Assistant
Java code to compare 2 jsons using recurssion
To compare two JSONs using recursion in Java, you can create a recursive method that iterates through each object and property of the JSONs. Here's an example code:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonComparator {
public static boolean compareJson(JSONObject json1, JSONObject json2) throws JSONException {
// Check if both JSON objects are null
if (json1 == null && json2 == null) {
return true;
}
// Check if only one JSON object is null
if (json1 == null || json2 == null) {
return false;
}
// Check if the length of the JSON objects is different
if (json1.length() != json2.length()) {
return false;
}
// Iterate through each key of the JSON objects
for (String key : json1.keySet()) {
// Check if the second JSON object contains the same key
if (!json2.has(key)) {
return false;
}
// Get the values associated with the key
Object value1 = json1.get(key);
Object value2 = json2.get(key);
// Recursively compare nested JSON objects or arrays
if (value1 instanceof JSONObject && value2 instanceof JSONObject) {
if (!compareJson((JSONObject) value1, (JSONObject) value2)) {
return false;
}
} else if (value1 instanceof JSONArray && value2 instanceof JSONArray) {
if (!compareJsonArray((JSONArray) value1, (JSONArray) value2)) {
return false;
}
} else {
// Check if the values are equal
if (!value1.equals(value2)) {
return false;
}
}
}
return true;
}
public static boolean compareJsonArray(JSONArray array1, JSONArray array2) throws JSONException {
// Check if both JSON arrays are null
if (array1 == null && array2 == null) {
return true;
}
// Check if only one JSON array is null
if (array1 == null || array2 == null) {
return false;
}
// Check if the length of the JSON arrays is different
if (array1.length() != array2.length()) {
return false;
}
// Iterate through each element of the JSON arrays
for (int i = 0; i < array1.length(); i++) {
Object value1 = array1.get(i);
Object value2 = array2.get(i);
// Recursively compare nested JSON objects or arrays
if (value1 instanceof JSONObject && value2 instanceof JSONObject) {
if (!compareJson((JSONObject) value1, (JSONObject) value2)) {
return false;
}
} else if (value1 instanceof JSONArray && value2 instanceof JSONArray) {
if (!compareJsonArray((JSONArray) value1, (JSONArray) value2)) {
return false;
}
} else {
// Check if the values are equal
if (!value1.equals(value2)) {
return false;
}
}
}
return true;
}
public static void main(String[] args) throws JSONException {
String json1 = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
String json2 = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
JSONObject jsonObject1 = new JSONObject(json1);
JSONObject jsonObject2 = new JSONObject(json2);
boolean isEqual = compareJson(jsonObject1, jsonObject2);
System.out.println("Are the JSONs equal? " + isEqual);
}
}
In this example, the compareJson method recursively compares two JSON objects, while the compareJsonArray method recursively compares two JSON arrays. The main method demonstrates a usage example by comparing two JSONs. The output will be Are the JSONs equal? true if the JSONs are equal, and Are the JSONs equal? false if they are not equal.