如何存储JSON包含嵌套的JSONArray

时间:2014-04-30 10:53:50

标签: java android arraylist hashmap

我正在开发一个应用程序,其中JSON赞...

    {
    "Group Details":
    [
        {
        "groupMasterId": "25",
        "GroupName": "Gangs of Beasts",
        "groupIcon": "http://xxx.xxx.xx.x/XYZ/images/evil.png",
        "GroupDescription": "Fellowzzzgroup",
        "GroupCreatedDate": "2014-04-30 15:41:01",
        "GroupMassage": "no such thing is like group msg",
        "UserData":
            [
            {"UserMobile": "1111111111","AdminFlag": "1"},
            {"UserMobile": "1234567890","AdminFlag": "0"},
            {"UserMobile": "9988776655","AdminFlag": "0"},
            {"UserMobile": "234t537535","AdminFlag": "0"},
            {"UserMobile": "3489869348","AdminFlag": "0"},
            {"UserMobile": "1234567890","AdminFlag": "0"}
            ]
        }
    ]
}

我想在TextView中显示关于简单ListView和UserData的详细信息..但是我的问题完全与如何存储这些数据相混淆..我完成了以下代码来完成它, ,但无法将所有UserMobile存储在单HashMap ...

JSONObject jsonObj = new JSONObject(jsonstr);
            GroupDetailsArray = jsonObj.getJSONArray(GROUPDETAILS);
            HashMap<String, String> groupuser = new HashMap<String, String>();
            for (int i = 0; i < GroupDetailsArray.length(); i++) {
                JSONObject l = GroupDetailsArray.getJSONObject(i);
                // String ID= l.getString(GROUPMASTERID);
                String NAME = l.getString(GROUPNAME);
                String DESCRIPTION = l.getString(GROUPDESCRIPTION);
                String DATE = l.getString(GROUPCREATEDDATE);
                String ICON = l.getString(GROUPICON);
                JSONArray UsedDataArray = l.getJSONArray(USERDATA);
                String ADMIN;
                for (int j = 0; j < UsedDataArray.length(); j++) {

                    JSONObject l1 = UsedDataArray.getJSONObject(j);
                    if (l1 != null) {
                        String MOBILENUMBER = l1.getString(USERMOBILE);

                        if (l1.getString(ADMINFLAG).equals("0")) {
                            ADMIN = "Member";
                        } else {
                            ADMIN = "Admin";
                        }
                        // tmp hashmap for single group
                        groupuser.put(USERMOBILE, MOBILENUMBER);
                        groupuser.put(ADMINFLAG, ADMIN);
                    }
                }
                // tmp hashmap for single group
                HashMap<String, String> group = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                group.put(GROUPNAME, NAME);
                group.put(GROUPDESCRIPTION, DESCRIPTION);
                group.put(GROUPCREATEDDATE, DATE);
                group.put(USERDATA, groupuser.toString());
                group.put(GROUPICON, ICON);
                // adding group to group list
                Utility.groupdetailsarraylist.add(group);
                Log.d("groupdetailsarraylist",
                        Utility.groupdetailsarraylist.toString());
            }

请帮我了解如何存储数据,以便我可以使用它存储在下一个Activity ...

1 个答案:

答案 0 :(得分:0)

       List list1=new ArrayList<String>();
    JSONArray array=jso.getJSONArray("UserData");
    for(int i=0;i<array.length();i++)
    {                                   list1.add(array.getJSONObject(i).getString("UserMobile"));

    }
   Iterator itr=list1.iterator();
     while(itr.hasNext())
    {
        str1=itr.next().e+"\n";

    }