2017-05-01 9 views
0

我定义了一个新的JSONObject和获得它从另一个JsonArray在我的应用程序的内容。但代码流时,jsonArray是不情愿改变每当JSONObject的变化..的Android的JSONObject自动更新它的父

的jsonArray被静态的,这可能是一个原因吗?但我需要它静态用于其他目的..

如何更改jsonObject而不更改jsonArray? 在此先感谢..

public class MyClass extends AppCompatActivity{ 
    private JSONArray jsonArray; 
    private JSONObject jsonObject; 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
     jsonArray=OtherActivity.jArray; //static array from other activity 
     jsonObject=new JSONObject(); 
     try { 
      if(jsonArray.getJSONObject(position).has(TAG)) { 
       jsonObject = jsonArray.getJSONObject(position).getJSONObject(TAG); 
      } 
     } catch (JSONException e) {   e.printStackTrace();  } 


    //..... 

    jsonObject.put("sth","someValue"); //jsonArray also changing as soon as jsonObject changes 

    } 
} 

回答

0

这是因为您将它分配给jsonArray对象。

jsonObject = jsonArray.getJSONObject(position).getJSONObject(TAG); 

您应该将对象克隆到新的JsonObject中。你可以做这样的事情....

JsonObject temp = jsonArray.getJSONObject(position).getJSONObject(TAG); 
JSONObject jsonObject = new JSONObject(temp, JSONObject.getNames(temp)); 
+0

我明白了..谢谢.. – Ahmed