【问题标题】:how to change the name of JSONObject dynamically如何动态更改 JSONObject 的名称
【发布时间】:2014-02-20 15:09:55
【问题描述】:

我很少有用户在其中输入详细信息的对话框(个人详细信息-名字、姓氏等),我需要捕获详细信息并转换为 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();
        }
}

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

【问题讨论】:

  • 您可以使用JSONObject 的列表,并为每个新人添加一个新元素到列表中。
  • 你能推荐任何 JSONObject 列表的链接或示例

标签: android json arrays jsonobject


【解决方案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();
                }

列表实现看起来像这样。

【讨论】:

  • 非常感谢。这正是我想要的。一个问题。用户可以删除一个人,在这种情况下,我如何从列表中删除一个特定的人,比如说 person1 需要被删除
  • 删除使用mMyList.remove(),可以按位置删除,也可以按JSONObject删除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多