
本文将详细介绍如何使用Java代码将两个JSONArray中的数据进行合并,并生成一个新的JSONArray。假设我们有两个JSONArray,它们之间通过一个公共的键(例如 "id")相关联。我们的目标是根据这个关联键,将两个JSONArray中的JSONObject进行合并,最终得到一个包含合并后的键值对的新JSONArray。
首先,我们需要明确JSONArray和JSONObject的概念。JSONArray可以看作是一个JSONObject的数组,而JSONObject则是一个键值对的集合。
实现步骤
准备数据: 首先,你需要准备好两个JSONArray。假设第一个JSONArray包含 "name" 和 "id" 字段,第二个JSONArray包含 "color"、"id" 和 "country" 字段。
立即学习“Java免费学习笔记(深入)”;
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONArrayMerger {
public static void main(String[] args) {
JSONArray array1 = new JSONArray();
array1.put(new JSONObject().put("name", "John").put("id", "1"));
array1.put(new JSONObject().put("name", "Adam").put("id", "2"));
JSONArray array2 = new JSONArray();
array2.put(new JSONObject().put("color", "red").put("id", "1").put("country", "Poland"));
array2.put(new JSONObject().put("color", "green").put("id", "2").put("country", "Germany"));
array2.put(new JSONObject().put("color", "red").put("id", "3").put("country", "England"));
JSONArray mergedArray = mergeJSONArrays(new JSONArray[]{array1, array2}, "id");
System.out.println(mergedArray.toString(2)); // 使用toString(2)格式化输出,方便阅读
}创建合并方法: 创建一个方法,该方法接收两个JSONArray和一个关联键作为参数。该方法将返回合并后的JSONArray。
public static JSONArray mergeJSONArrays(JSONArray[] arraysToMerge, String key) {
Map<String, JSONObject> merged = new HashMap<>();
for (JSONArray array : arraysToMerge) {
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.optJSONObject(i);
if (obj != null) {
String id = obj.optString(key);
if (id != null) {
JSONObject existingObj = merged.computeIfAbsent(id, (k) -> {
obj.remove(key);
return obj;
});
for (String name : JSONObject.getNames(obj)) {
if (!name.equals(key)) {
try {
existingObj.put(name, obj.get(name));
} catch (JSONException e) {
// 处理JSONException,例如打印错误信息
e.printStackTrace();
}
}
}
}
}
}
}
return new JSONArray(merged.values());
}遍历JSONArray并合并JSONObject: 在合并方法中,首先创建一个 HashMap 用于存储合并后的JSONObject。键是关联键的值,值是合并后的JSONObject。然后,遍历每个JSONArray,对于每个JSONObject,提取关联键的值。如果该值已经存在于 HashMap 中,则将当前JSONObject的键值对添加到已存在的JSONObject中。否则,创建一个新的JSONObject,并将当前JSONObject的键值对添加到新的JSONObject中,并将新的JSONObject添加到 HashMap 中。
创建新的JSONArray: 最后,将 HashMap 中的所有JSONObject转换为JSONArray,并返回该JSONArray。
完整代码示例
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import java.util.HashMap;
import java.util.Map;
public class JSONArrayMerger {
public static void main(String[] args) {
JSONArray array1 = new JSONArray();
try {
array1.put(new JSONObject().put("name", "John").put("id", "1"));
array1.put(new JSONObject().put("name", "Adam").put("id", "2"));
JSONArray array2 = new JSONArray();
array2.put(new JSONObject().put("color", "red").put("id", "1").put("country", "Poland"));
array2.put(new JSONObject().put("color", "green").put("id", "2").put("country", "Germany"));
array2.put(new JSONObject().put("color", "red").put("id", "3").put("country", "England"));
JSONArray mergedArray = mergeJSONArrays(new JSONArray[]{array1, array2}, "id");
System.out.println(mergedArray.toString(2)); // 使用toString(2)格式化输出,方便阅读
} catch (JSONException e) {
e.printStackTrace();
}
}
public static JSONArray mergeJSONArrays(JSONArray[] arraysToMerge, String key) {
Map<String, JSONObject> merged = new HashMap<>();
for (JSONArray array : arraysToMerge) {
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.optJSONObject(i);
if (obj != null) {
String id = obj.optString(key);
if (id != null) {
JSONObject existingObj = merged.computeIfAbsent(id, (k) -> {
obj.remove(key);
return obj;
});
for (String name : JSONObject.getNames(obj)) {
if (!name.equals(key)) {
try {
existingObj.put(name, obj.get(name));
} catch (JSONException e) {
// 处理JSONException,例如打印错误信息
e.printStackTrace();
}
}
}
}
}
}
}
return new JSONArray(merged.values());
}
}注意事项
总结
本文介绍了如何使用Java代码将两个JSONArray中的数据进行合并,并生成一个新的JSONArray。通过使用 HashMap 数据结构,可以方便地根据关联键将JSONObject进行合并。该方法可以应用于各种需要合并JSONArray数据的场景。请根据你的实际需求,修改和扩展该代码示例。
以上就是从Java中的JSONArray集合中提取键值对并合并为一个JSONArray的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号