2014-04-30 38 views
1

我工作的一個應用程序中,我有JSON如..如何存儲JSONcontaining嵌套JSONArray

{ 
    "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"} 
      ] 
     } 
    ] 
} 

我想在ListView顯示簡單TextView名和userdata詳細..但在我的proble在如何存儲這些數據。我做了下面的代碼做「M總迷茫,,但不能將所有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 ...

+0

你在'for循環中獲得什麼?這是什麼'JSONObject l1 = UsedDataArray.getJSONObject(j);'給? –

+0

在內循環我得到最後一個用戶移動和管理標誌,並將它們存儲在HashMap中,但他們得到覆蓋.. – qwerty

+0

@BharathMg我能夠以適當的方式解析數據,但是..我混淆了數據類型以供使用來存儲那個手機號碼.. – qwerty

回答

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"; 

    } 
+0

但是我想把UserMobile和AdminFlag放在一起.. – qwerty

+0

用戶手機是唯一的,用hashmap代替arraylist – Boopathi

+0

我想我用的是.. – qwerty