如何动态更改JSONObject的名称

时间:2022-04-21 18:59:58

I have few dialog's in which the user enters their details(person details- first name, last name etc..), I need to capture the details and convert to JSON. User can enter more than one person details like. person1, person2, person3(which should be the JSONObjects). When ever the bellow function is called for the first time I want JSONObject personJSON1 = new JSONObject(); second time its called JSONObject personJSON2 = new JSONObject(); and so on. Could you please suggest how can i achieve this.

我没有几个对话框,用户输入他们的详细信息(人员详细信息 - 名字,姓氏等),我需要捕获详细信息并转换为JSON。用户可以输入多个人的详细信息。 person1,person2,person3(应该是JSONObjects)。当第一次调用bellow函数时,我想要JSONObject personJSON1 = new JSONObject();第二次调用它的JSONObject personJSON2 = new 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();
        }
}

Your help/suggestions much appreciated. Thank you

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

1 个解决方案

#1


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

Doing the list implementation would look something like this.

执行列表实现看起来像这样。

#1


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

Doing the list implementation would look something like this.

执行列表实现看起来像这样。