search

Home  >  Q&A  >  body text

java - 两个 JSONObject 合并的问题

最近使用 org.json.JSONObject 封装json数据,现在遇到想把两个 JSONObject 进行合并不知道怎么弄了,请教各位老司机指点一下。

import org.json.JSONObject;

JSONObject json1 = new JSONObject();
JSONObject json2 = new JSONObject();
        
json1.put("aa", 1111);
json1.put("bb", 2222);
        
json2.put("cc", 3333);
json2.put("dd", 4444);
        
System.out.println(json2);

想要得到json1与json2合并后的json格式,有没有相应的方法啊,我看了下API没找到。

{"aa":1111,"bb":2222,"cc":3333,"dd":4444}
巴扎黑巴扎黑2889 days ago520

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:51:38

    Isn’t it enough to loop through all the keys and values ​​of json2 and then put them into json1? ^_^

    reply
    0
  • Cancelreply