2015-06-10 40 views
-2

部分我有一个像下面拆分列表视图进入使用如下JSON响应

{ 
     "continent":"Africa", 
     "name":"Djezzy", 
     "cid":"3", 
     "country":"Algeria", 
     "filename":"djezzy.png", 
     "iso2":"DZ", 
     "iso3":"DZA", 
     "network":"OTA NET, ALG 02, 603 02" 
    }, 
    { 
     "continent":"Africa", 
     "name":"Etisalat Nigeria", 
     "cid":"156", 
     "country":"Nigeria", 
     "filename":"etisalat.png", 
     "iso2":"NG", 
     "iso3":"NGA", 
     "network":"Etisalat" 
    }, 
    { }, 
    { 
     "continent":"Americas", 
     "name":"Tigo", 
     "cid":"47", 
     "country":"Colombia", 
     "filename":"tigo.png", 
     "iso2":"CO", 
     "iso3":"COL", 
     "network":"Tigo" 
    }, 
    { 
     "continent":"Europe", 
     "name":"Beeline-Armenia", 
     "cid":"11", 
     "country":"Armenia", 
     "filename":"beeline.png", 
     "iso2":"AM", 
     "iso3":"ARM", 
     "network":"Beeline, ArmenTel, ARMGSM, ARM 01, 283 01" 
    }, 
    { 
     "continent":"Europe", 
     "name":"life", 
     "cid":"220", 
     "country":"Ukraine", 
     "filename":"life.png", 
     "iso2":"UA", 
     "iso3":"UKR", 
     "network":"UA Astelit, UKR 06, 255 06" 
    }, 
    { 
     "continent":"Europe", 
     "name":"T-Mobile", 
     "cid":"240", 
     "country":"Montenegro", 
     "filename":"tmobile.png", 
     "iso2":"ME", 
     "iso3":"MNE", 
     "network":"T-Mobile CG, YU 04, 220 04, 297 02, MNE 02" 
    }, 
    { 
     "continent":"Europe", 
     "name":"Turkcell", 
     "cid":"215", 
     "country":"Turkey", 
     "filename":"turkcell.png", 
     "iso2":"TR", 
     "iso3":"TUR", 
     "network":"Turkcell" 
    }, 
    { 
     "continent":"Middle East & Asia", 
     "name":"3hk", 
     "cid":"96", 
     "country":"Hong Kong", 
     "filename":"3hk.png", 
     "iso2":"HK", 
     "iso3":"HKG", 
     "network":"3, 3G, Orange, 3-Dualband" 
    } 

现在我要显示一个国家的列表,将列表分为大陆明智段要求的JSON响应。我可以使用Expandable列表视图,但是如何将这个json分成多个数组列表,然后我可以在可扩展列表视图中将其用作组和子组。

为了更好undertanding我需要显示这样的数据... https://drive.google.com/a/panzertechnologies.net/file/d/0B7Jo72tgHOJUVjVLS0cwVWhaUzQ/view?usp=sharing

在此先感谢。

+0

您是否尝试使用Google搜索JSON解析库? – Codebender

+1

只需检查你的json。我正在检查它到任何在线json验证器或格式化程序,并且它不正确 – Pankaj

+0

您有json对象,现在您可以制作一个getter setter并将其添加到arraylist。使用它你可以将数组数据显示到列表视图中。 – Shvet

回答

0

尝试这样的,它可以帮助你

initilaize字符串数组

String[] CENTERNAME,CENTER_ID; 

越来越JSON数组

//Converting string to Array list 
        ArrayList<String> centername_arr= new ArrayList<String>(); 
        ArrayList<String> Center_Id_arr= new ArrayList<String>(); 


if ((response.toString()).contains("{")) 
      { 
       // JSONArray jr = new JSONArray(response); 
       SoapObject rep = (SoapObject) envelope.bodyIn; 
       JSONArray jr = new JSONArray(rep.getPropertyAsString(0)); 
       for (int i = 0; i < jr.length(); i++) 
       { 
        JSONObject jb = (JSONObject) jr.get(i); 


         Center_id = jb.getString("CenterId"); 
         CenterName = jb.getString("CenterName"); 


          centername_arr.add(CenterName); 
          Center_Id_arr.add(Center_id); 

}} 

//Convert Arraylist to String Array 
CENTERNAME = new String[centername_arr.size()]; 
CENTERNAME = centername_arr.toArray(CENTERNAME); 

CENTER_ID= new String[Center_Id_arr.size()]; 
CENTER_ID = Center_Id_arr.toArray(CENTER_ID); 

那么你可以使用字符串数组之前初始化数组列表您Listview

+0

非常感谢马诺抽出时间来帮助我。我得到的解决方案... :) – Aziz

+0

如果我的答案可以帮助你,只需接受答案:-) – Mano

+0

我正要upvote你,但不幸的是它需要15名声望upvote,我只有6 ...我再次想要感谢你@Mano ... :) – Aziz

0

谢谢大家花一些时间阅读我的q uestion ..我找到了答案,下面是解决方案。

JSONObject j=jo.getJSONObject("response"); 
    JSONArray jar=j.getJSONArray("data"); 
    for (int i = 0; i < jar.length(); i++) { 

    JSONObject job=jar.getJSONObject(i); 
    String continent=job.getString("continent"); 
    String name=job.getString("name"); 
    String country=job.getString("country"); 
     // tmp hashmap for single contact 
       HashMap<String, String> contact = new HashMap<String, String>(); 
       boolean exists = false; 
    for (HashMap<String, String> hashMap : contactList) { 
     try { 
     if (hashMap.get("continent").equals(continent)) { 
     exists = true; 
     break; 
     } 
     } catch (Exception e) { 
     } 
    } 
    if (!exists) { 
     contact.put("continent", continent); 
    } 

       contact.put("name", name); 
       contact.put("country", country); 

       contactList.add(contact); 

    }