我们如何在Java中合并两个JSON对象?
我们可以使用 putAll() 合并两个 JSON 对象下面的程序中的 方法(继承自接口 java.util.Map)。
示例
import java.util.Date; import org.json.simple.JSONObject; public class MergeJsonObjectsTest { public static void main(String[] args) { JSONObject jsonObj = new JSONObject(); // first json object jsonObj.put("Name", "Adithya"); jsonObj.put("Age", 25); jsonObj.put("Address", "Hitech City"); JSONObject jsonObj1 = new JSONObject(); // second json object jsonObj1.put("City", "Hyderabad"); jsonObj1.put("DOB", new Date(104, 3, 6)); jsonObj.putAll(jsonObj1); // merging of first and second json objects System.out.println(jsonObj); } }登录后复制