How do I clone an org.json.JSONObject in Java?

巧了我就是萌 提交于 2019-12-03 02:03:06

Use the public JSONObject(JSONObject jo, java.lang.String[] names) constructor and the public static java.lang.String[] getNames(JSONObject jo) method.

JSONObject copy = new JSONObject(original, JSONObject.getNames(original));
Jaytjuh

Easiest (and incredibly slow and inefficient) way to do it

JSONObject clone = new JSONObject(original.toString());

Couldn't find an existing deep clone method for com.google.gwt.json.client.JSONObject but the implementation should be few lines of code, something like:

public static JSONValue deepClone(JSONValue jsonValue){
    JSONString string = jsonValue.isString();
    if (string != null){return new JSONString(string.stringValue());}

    JSONBoolean aBoolean = jsonValue.isBoolean();
    if (aBoolean != null){return JSONBoolean.getInstance(aBoolean.booleanValue());}

    JSONNull aNull = jsonValue.isNull();
    if (aNull != null){return JSONNull.getInstance();}

    JSONNumber number = jsonValue.isNumber();
    if (number!=null){return new JSONNumber(number.doubleValue());}

    JSONObject jsonObject = jsonValue.isObject();
    if (jsonObject!=null){
        JSONObject clonedObject = new JSONObject();
        for (String key : jsonObject.keySet()){
            clonedObject.put(key, deepClone(jsonObject.get(key)));
        }
        return clonedObject;
    }

    JSONArray array = jsonValue.isArray();
    if (array != null){
        JSONArray clonedArray = new JSONArray();
        for (int i=0 ; i < array.size() ; ++i){
            clonedArray.set(i, deepClone(array.get(i)));
        }
        return clonedArray;
    }

    throw new IllegalStateException();
}

*Note:*I haven't tested it yet!

Cause $JSONObject.getNames(original) not accessible in android, you can do it with:

public JSONObject shallowCopy(JSONObject original) {
    JSONObject copy = new JSONObject();

    for ( Iterator<String> iterator = original.keys(); iterator.hasNext(); ) {
        String      key     = iterator.next();
        JSONObject  value   = original.optJSONObject(key);

        try {
            copy.put(key, value);
        } catch ( JSONException e ) {
            //TODO process exception
        }
    }

    return copy;
}

But remember it is not deep copy.

For Android developers, the simplest solution without using .getNames is:

JSONObject copy = new JSONObject();
for (Object key : original.keySet()) {
  Object value = original.get(key);
  copy.put(key, value);
}

Note: This is a only a shallow copy

In case anyone comes here looking for a deep clone for org.google.gson, since they don't expose their deepClone() method this is what I came up with...

public static JsonElement deepClone(JsonElement el){
    if (el.isJsonPrimitive() || el.isJsonNull())
        return el;
    if (el.isJsonArray()) {
        JsonArray array = new JsonArray();
        for(JsonElement arrayEl: el.getAsJsonArray())
            array.add(deepClone(arrayEl));
        return array;
    }
    if(el.isJsonObject()) {
        JsonObject obj = new JsonObject();
        for (Map.Entry<String, JsonElement> entry : el.getAsJsonObject().entrySet()) {
            obj.add(entry.getKey(), deepClone(entry.getValue()));
        }
        return obj;
    }
    throw new IllegalArgumentException("JsonElement type " + el.getClass().getName());
}

And here are a few methods to merge two JsonObject's

public static JsonObject merge(String overrideJson, JsonObject defaultObj) {
    return mergeInto((JsonObject)new JsonParser().parse(overrideJson), defaultObj);
}
public static JsonObject merge(JsonObject overrideObj, JsonObject defaultObj) {
    return mergeOverride((JsonObject)deepClone(defaultObj), overrideObj);
}
public static JsonObject mergeOverride(JsonObject targetObj, JsonObject overrideObj) {
    for (Map.Entry<String, JsonElement> entry : overrideObj.entrySet())
            targetObj.add(entry.getKey(), deepClone(entry.getValue()));
    return targetObj;
}
public static JsonObject mergeInto(JsonObject targetObj, JsonObject defaultObj) {
    for (Map.Entry<String, JsonElement> entry : defaultObj.entrySet()) {
        if (targetObj.has(entry.getKey()) == false)
            targetObj.add(entry.getKey(), deepClone(entry.getValue()));
    }
    return targetObj;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!