2015-02-10 90 views
1
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; 
} 

嗨更换的JSONObject,我想与new Value(newJSON)返回父,但父没有它,tmp不改变价值。中的JSONObject

回答

0

那么你可以尝试处理它作为一个字符串,只需将您的新对象插入父。

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; 
} 

这个,如果你已经添加子对象(因为我们添加了一个逗号)之前有家长里面的,至少1个键只会工作,但你可以照顾 与简单的IF

编辑: 实际上更好的方法是

JSONObject parent = new JSONObject(); 
JSONObject child = new JSONObject(); 
parent.put("object name",child);