import org.json.JSONException;
import org.json.JSONObject;
/**
* json深度合并
*
**/
public class JsonUtils {
/**
* 深度拷贝
* Merge "source" into "target". If fields have equal name, merge them recursively.
*
* @return the merged object (target).
*/
public static JSONObject deepMerge(JSONObject source, JSONObject target) throws JSONException {
for (String key : JSONObject.getNames(source)) {
Object value = source.get(key);
if (!target.has(key)) {
// new value for "key":
target.put(key, value);
} else {
// existing value for "key" - recursively deep merge:
if (value instanceof JSONObject) {
JSONObject valueJson = (JSONObject) value;
deepMerge(valueJson, target.getJSONObject(key));
} else {
target.put(key, value);
}
}
}
return target;
}
}
最后
以上就是花痴高山最近收集整理的关于【java】json深度合并(深度拷贝)的全部内容,更多相关【java】json深度合并(深度拷贝)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复