2
private JSONObject insertJSONintoJSON(JSONObject newJSON, JSONObject parent)
{
    Object[] a = parent.values().toArray();
    JSONObject jsonObject2 = (JSONObject) a[1];
    Object[] b = jsonObject2.values().toArray();
    JSONObject folders = (JSONObject) b[2];

    Object[] c = folders.values().toArray();
    JSONArray folderss = (JSONArray) c[2];

    for (Object objc : folderss)
    {
        JSONObject tmp = (JSONObject) objc;
        Object[] d = tmp.values().toArray();
        String name = (String) d[4];

        if (name.toUpperCase().equals("EXCHANGE"))
        {
            tmp = newJSON;
            return parent;
        }
    }

    return parent;
}

Hi, I want to return the parent with the new Value(newJSON), but the parent doesn't have it in, tmp doesn't change the value.

1 Answer 1

1

Well you could try handling it as a String and simply inserting your new object in the parent.

    private static JSONObject insertObj(JSONObject parent, JSONObject child){
    String parentStr = parent.toString();
    parentStr = parentStr.substring(1);//remove the opening curly bracket {
    String childStr = child.toString();
    childStr = childStr.substring(0, (childStr.length()-1));
    parentStr = childStr+","+parentStr;
    JSONObject resultObj = new JSONObject(parentStr);
    return resultObj;
}

this will only work if you already have at-least 1 key inside the parent before adding the child object (because we are adding a comma) but you can take care of that with a simple IF

EDIT: Actually a much better way would be

JSONObject parent = new JSONObject();
JSONObject child = new JSONObject();
parent.put("object name",child);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.