BRDroid
BRDroid

Reputation: 4388

how to change the name of JSONObject dynamically

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.

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

Upvotes: 0

Views: 137

Answers (1)

AlexMasca
AlexMasca

Reputation: 403

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.

Upvotes: 1

Related Questions