Android 如何动态更改JSONObject的名称
我有几个对话框,用户在其中输入他们的详细信息(个人详细信息-名字、姓氏等),我需要捕获详细信息并转换为JSON。 用户可以输入多个人的详细信息,如。person1、person2、person3(应该是JSONObject)。当我想第一次调用bellow函数时 JSONObject personJSON1=新的JSONObject(); 第二次电话 JSONObject personJSON2=新的JSONObject();等等 你能建议我如何做到这一点吗Android 如何动态更改JSONObject的名称,android,json,arrays,jsonobject,Android,Json,Arrays,Jsonobject,我有几个对话框,用户在其中输入他们的详细信息(个人详细信息-名字、姓氏等),我需要捕获详细信息并转换为JSON。 用户可以输入多个人的详细信息,如。person1、person2、person3(应该是JSONObject)。当我想第一次调用bellow函数时 JSONObject personJSON1=新的JSONObject(); 第二次电话 JSONObject personJSON2=新的JSONObject();等等 你能建议我如何做到这一点吗 private void person
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();
}
}
非常感谢你的帮助/建议。谢谢公共列表mMyList=new ArrayList();
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();
}
私有void personAdded(){
JSONObject personJSON=新的JSONObject();
JSONArray personArrayjson=新的JSONArray();
JSONObject PersonObject JSON=新的JSONObject();
试试{
personObjectJson.put(“otherFirstName”,sOtherFirstName);
personObjectJson.put(“otherLastName”,sOtherLastName);
personObjectJson.put(“其他地址”,sOtherAddress);
personObjectJson.put(“otherTown”,sOtherTown);
personObjectJson.put(“其他县”,sOtherCounty);
personObjectJson.put(“其他邮政编码”,其他邮政编码);
personObjectJson.put(“otherTelephone”,sOtherTelephone);
personObjectJson.put(“otherMobilePhone”,sOtherMobilePhone);
personObjectJson.put(“其他电子邮件”,sOtherEmail);
personObjectJson.put(“otherPersonInvolvement”,表示其他未解决的人);
mMyList.add(personJSON)
}捕获(JSONException e){
e、 printStackTrace();
}
执行列表实现看起来是这样的。您可以使用一个
JSONObject
列表,并为每个新人添加一个新元素。您能为JSONObject列表提供任何链接或示例吗非常感谢。这正是我要找的。这里有一个问题。用户可以删除一个人,在这里s case如何从特定人员的列表中删除让我们假设person1需要删除DAPI引用在这里帮助您:要删除,请使用mMyList.remove()
,您可以按位置或JSONObject删除。