2014-02-20 89 views
0

我有几个用户在其中输入他们的详细信息(人员详细信息 - 名字,姓氏等)的对话框,我需要捕获细节并转换为JSON。 用户可以输入多个人的详细信息。 person1,person2,person3(应该是JSONObjects)。当我第一次调用下面的函数时,我想要 JSONObject personJSON1 = new JSONObject(); 第二次调用 JSONObject personJSON2 = new JSONObject();等等。 你能否建议我怎么做到这一点。如何动态更改JSONObject的名称

private void personAdded() { 
JSONObject personJSON = new JSONObject(); 
     JSONArray personArrayjson = new JSONArray(); 
     JSONObject personObjectJson = new JSONObject(); 
     try {   
       personObjectJson.put("otherFirstName", sOtherFirstName); 
       personObjectJson.put("otherLastName", sOtherLastName); 
       personObjectJson.put("otherAddress", sOtherAddress); 
       personObjectJson.put("otherTown", sOtherTown); 
       personObjectJson.put("otherCounty", sOtherCounty); 
       personObjectJson.put("otherPostcode", sOtherPostcode); 
       personObjectJson.put("otherTelephone", sOtherTelephone); 
       personObjectJson.put("otherMobilePhone", sOtherMobilePhone);  
       personObjectJson.put("otherEmail", sOtherEmail); 
       personObjectJson.put("otherPersonInvolvement", sHowWasTheOtherPersonInvolved);   

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 
} 

非常感谢您的帮助/建议。谢谢

+0

您可以使用'JSONObject'列表,并为每个新人添加一个新元素到列表中。 – user3182577

+0

你能建议任何链接或例子列表JSONObject – BRDroid

回答

1
public List<JSONObject> mMyList = new ArrayList<JSONObject>(); 

    private void personAdded() { 
     JSONObject personJSON = new JSONObject(); 
       JSONArray personArrayjson = new JSONArray(); 
       JSONObject personObjectJson = new JSONObject(); 
       try {   
         personObjectJson.put("otherFirstName", sOtherFirstName); 
         personObjectJson.put("otherLastName", sOtherLastName); 
         personObjectJson.put("otherAddress", sOtherAddress); 
         personObjectJson.put("otherTown", sOtherTown); 
         personObjectJson.put("otherCounty", sOtherCounty); 
         personObjectJson.put("otherPostcode", sOtherPostcode); 
         personObjectJson.put("otherTelephone", sOtherTelephone); 
         personObjectJson.put("otherMobilePhone", sOtherMobilePhone);  
         personObjectJson.put("otherEmail", sOtherEmail); 
         personObjectJson.put("otherPersonInvolvement", sHowWasTheOtherPersonInvolved);   
     mMyList.add(personJSON) 
       } catch (JSONException e) { 

        e.printStackTrace(); 
       } 

做清单实现看起来像这样。

+0

非常感谢。这正是我期待的。其中一个问题。用户可以删除一个人,在这种情况下,如何从特定人员的列表中删除可以说person1需要删除 – BRDroid

+0

API参考在这里可以帮助您:http://developer.android.com/reference/java/ util/List.html – Submersed

+0

若要移除使用'mMyList.remove()',您可以按位置或JSONObject移除。 – user3182577