как динамически изменить имя JSONObject

У меня есть несколько диалогов, в которых пользователь вводит свои данные (данные о человеке - имя, фамилия и т. Д.), Мне нужно зафиксировать данные и преобразовать их в JSON. Пользователь может ввести данные более чем об одном человеке, например. person1, person2, person3 (которые должны быть JSONObjects). Когда функция ниже вызывается в первый раз, я хочу, чтобы 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();
        }
}

Ваша помощь/предложения очень ценятся. Спасибо


person BRDroid    schedule 20.02.2014    source источник
comment
Вы можете использовать список из JSONObject и для каждого нового человека добавлять в список новый элемент.   -  person AlexMasca    schedule 20.02.2014
comment
можете ли вы предложить какие-либо ссылки или примеры для списка JSONObject   -  person BRDroid    schedule 20.02.2014


Ответы (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();
                }

Реализация списка будет выглядеть примерно так.

person AlexMasca    schedule 20.02.2014
comment
Большое спасибо. Это именно то, что я искал. один вопрос в этом. Пользователь может удалить человека, в этом случае, как удалить из списка конкретного человека, скажем, нужно удалить человека 1. - person BRDroid; 20.02.2014
comment
Справочные материалы по API здесь, чтобы помочь вам: developer.android.com/reference/java/ утилита/List.html - person Submersed; 20.02.2014
comment
Чтобы удалить использование mMyList.remove(), вы можете удалить по положению или по JSONObject. - person AlexMasca; 20.02.2014