Java 组合(合并)2 个 JSONObjects 的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19566081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to combine (merge) 2 JSONObjects?
提问by jimpanzer
What is the best way to combine (merge) two JSONObjects
?
结合(合并)两个的最佳方法是什么JSONObjects
?
JSONObject o1 = {
"one": "1",
"two": "2",
"three": "3"
}
JSONObject o2 = {
"four": "4",
"five": "5",
"six": "6"
}
And result of combining o1
and o2
must be
和组合的结果o1
并且o2
必须是
JSONObject result = {
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6"
}
回答by Nirav Tukadiya
json objects to be merge in that new json object like this.
json 对象要合并到这样的新 json 对象中。
JSONObject jObj = new JSONObject();
jObj.put("one", "1");
jObj.put("two", "2");
JSONObject jObj2 = new JSONObject();
jObj2.put("three", "3");
jObj2.put("four", "4");
JSONParser p = new JSONParser();
net.minidev.json.JSONObject o1 = (net.minidev.json.JSONObject) p
.parse(jObj.toString());
net.minidev.json.JSONObject o2 = (net.minidev.json.JSONObject) p
.parse(jObj2.toString());
o1.merge(o2);
Log.print(o1.toJSONString());
now o1 will be the merged json object. you will get the output like this ::
现在 o1 将是合并的 json 对象。你会得到这样的输出::
{"three":"3","two":"2","four":"4","one":"1"}
please refer this link and download the smartjson library ..here is the link http://code.google.com/p/json-smart/wiki/MergeSample
请参考此链接并下载 smartjson 库..这是链接http://code.google.com/p/json-smart/wiki/MergeSample
hope it will help.
希望它会有所帮助。
回答by Ankur
Try this.. hope it helps
试试这个..希望它有帮助
JSONObject result = new JSONObject();
result.putAll(o1);
result.putAll(O2);
回答by Vito Gentile
I have your same problem: I can't find the putAll
method (and it isn't listed in the official reference page).
我有你同样的问题:我找不到putAll
方法(它没有在官方参考页面中列出)。
So, I don't know if this is the best solution, but surely it works quite well:
所以,我不知道这是否是最好的解决方案,但肯定效果很好:
//I assume that your two JSONObjects are o1 and o2
JSONObject mergedObj = new JSONObject();
Iterator i1 = o1.keys();
Iterator i2 = o2.keys();
String tmp_key;
while(i1.hasNext()) {
tmp_key = (String) i1.next();
mergedObj.put(tmp_key, o1.get(tmp_key));
}
while(i2.hasNext()) {
tmp_key = (String) i2.next();
mergedObj.put(tmp_key, o2.get(tmp_key));
}
Now, the merged JSONObject is stored in mergedObj
现在,合并的 JSONObject 存储在 mergedObj
回答by tainy
How about this:
这个怎么样:
Iterator iterator = json2.keys();
while(iterator.hasNext()){
String key = iterator.next().toString();
json1.put(key,map.optJSONObject(key));
}
回答by Abhishek singh
Merge JsonObject(gson)-
合并 JsonObject(gson)-
JsonObject data = new JsonObject();
data = receivedJsoData.get("details").getAsJsonObject();
JsonObject data2 = new JsonObject();
data2 = receivedJsoData1.get("details").getAsJsonObject();
JsonObject mergedData = new JsonObject();
Set<Map.Entry<String, JsonElement>> entries = data1.entrySet(); //will return members of your object
for (Map.Entry<String, JsonElement> entry : entries) {
mergedData.add(entry.getKey(), entry.getValue());
}
Set<Map.Entry<String, JsonElement>> entries1 = data2.entrySet(); //will return members of your object
for (Map.Entry<String, JsonElement> entry : entries1) {
mergedData.add(entry.getKey(), entry.getValue());
}