-2

I have a json response like below

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

Now I have a requirement to display the list of countries and divide the list into continent wise sections. I can use Expandable List View but how can I divide this json into multiple array lists which I could then use as group and child in expandable list view.

For better undertanding I need to display the data like this... https://drive.google.com/a/panzertechnologies.net/file/d/0B7Jo72tgHOJUVjVLS0cwVWhaUzQ/view?usp=sharing

Thanks in advance.

Aziz
  • 234
  • 1
  • 5
  • Did you try googling for a JSON parsing library??? – Codebender Jun 10 '15 at 09:24
  • 1
    Just check your json. I am checking it to any online json validator or formatter and its not correct – Pankaj Jun 10 '15 at 09:26
  • you have json object and now you can make one getter setter and add it to arraylist. using it you can show array of data into listview. – Shvet Jun 10 '15 at 09:29
  • It should be jsonArray......missing '[' and ']' in start and end respectively – Narendra Singh Jun 10 '15 at 09:59
  • Now I have a requirement to display the list of countries and divide the list into continent wise sections. I can use Expandable List View but how can I divide this json into multiple array lists which I could then use as group and child in expandable list view. – Aziz Jun 10 '15 at 12:12

2 Answers2

0

try like this , it May help you

initilaize the string array

String[] CENTERNAME,CENTER_ID;

initialize the arraylist before getting json array

//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);

Then You Can use the String Array in your Listview

Android Dev
  • 421
  • 8
  • 26
0

Thank you all for spending some time on reading my question.. I found the answer and below is the solution.

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

    }
Aziz
  • 234
  • 1
  • 5