0

Below is my json code, I can't convert it to arraylist:

{"location":[{"place_name":"Arabian Ranches ,Dubai Land"},{"place_name":"The Greens & The Views ,Emirates Living"},{"place_name":"Downtown Dubai ,Dubai"},{"place_name":"Dubai Marina ,Dubai"},{"place_name":"Dubai Land ,Dubai"},{"place_name":"Dubai Sports City ,Dubai Land"},{"place_name":"JBR Jumeirah Beach Residence ,Dubai"},{"place_name":"JLT Jumeirah Lake Towers ,Dubai"},{"place_name":"Jumeirah Village Circle ,Jumeirah Village"},{"place_name":"Down Town Jebel Ali ,Waterfront Jebel Ali"},{"place_name":"Business Bay ,Dubai"},{"place_name":"Trade Centre ,Dubai"},{"place_name":"Jumeirah Village Triangle ,Jumeirah Village"},{"place_name":"Jumeirah Park ,Dubai"},{"place_name":"The Springs ,Emirates Living"},{"place_name":"Victory Heights ,Dubai Sports City"},{"place_name":"The Villa ,Dubai Land"},{"place_name":"City of Arabia ,Dubai Land"},{"place_name":"Mirdif ,Dubai"},{"place_name":"DIFC ,Dubai"},{"place_name":"Al Quoz ,Dubai"},{"place_name":"Mudon ,Dubai Land"},{"place_name":"Uptown Mirdif ,Mirdif"},{"place_name":"Arjan ,Dubai"},{"place_name":"Al Sufouh ,Dubai"},{"place_name":"Discovery Gardens ,Jebel Ali Village"},{"place_name":"Al Mamzar ,Dubai"},{"place_name":"Old Town ,Downtown Dubai"},{"place_name":"Mohammed Bin Rashid Al Maktoum City ,Dubai"},{"place_name":"Al Barsha South 1 ,Al Barsha"},{"place_name":"The Meadows ,Emirates Living"},{"place_name":"The Address Dubai Mall ,Downtown Dubai"},{"place_name":"Dubai Silicon Oasis ,Dubai"},{"place_name":"Al Furjan ,Jebel Ali Village"},{"place_name":"The Palm Jumeirah ,Dubai"},{"place_name":"Jumeirah Golf Estates ,Dubai"},{"place_name":"Al Barsha 1 ,Al Barsha"},{"place_name":"Falcon City ,Dubai Land"},{"place_name":"Mira ,Reem"},{"place_name":"The Hills ,Emirates Living"},{"place_name":"Executive Towers ,Business Bay"},{"place_name":"IMPZ International Media Production Zone ,Dubai"},{"place_name":"Up Town Motor City ,Motor City"},{"place_name":"Emirates Living ,Dubai"},{"place_name":"Reem ,Dubai Land"},{"place_name":"The Lakes ,Emirates Living"},{"place_name":"Dubai Hills Estate ,Dubai"},{"place_name":"International City ,Dubai"},{"place_name":"Culture Village ,Al Jadaf"},{"place_name":"Bur Dubai ,Dubai"},{"place_name":"Jumeirah 2 ,Jumeirah"},{"place_name":"Umm Suqeim ,Dubai"},{"place_name":"Jumeirah 1 ,Jumeirah"},{"place_name":"Meydan City ,Mohammed Bin Rashid Al Maktoum City"},{"place_name":"Dubai Lagoon ,The Lagoons"},{"place_name":"Al Khawaneej ,Dubai"},{"place_name":"DIFC ,Zabeel"},{"place_name":"Al Warsan ,International City"},{"place_name":"Al Wasl ,Dubai"},{"place_name":"Al Safa 1 ,Al Safa"},{"place_name":"Jumeirah 3 ,Jumeirah"},{"place_name":"Hattan 1 ,The Lakes"},{"place_name":"Al Barari ,Dubai"},{"place_name":"Layan Community ,Dubai Land"},{"place_name":"Waterfront Jebel Ali ,Dubai"},{"place_name":"Sheikh Zayed Road ,Dubai"},{"place_name":"Emirates Hills ,Emirates Living"},{"place_name":"Sobha Hartland ,Mohammed Bin Rashid Al Maktoum City"},{"place_name":"Akoya ,Dubai Land"},{"place_name":"Barsha Heights (Tecom) ,Dubai"},{"place_name":"Al Bada ,Dubai"},{"place_name":"The World ,Dubai"},{"place_name":"Dubai Festival City ,Dubai"},{"place_name":"Pearl Jumeirah ,Dubai"},{"place_name":"Al Barsha ,Dubai"},{"place_name":"Al Twar ,Dubai"},{"place_name":"Dubai Investment Park ,Dubai"},{"place_name":"Jumeirah Islands ,Dubai"},{"place_name":"JAFZA Jebel Ali Free Zone ,Jebel Ali"},{"place_name":"Dubai Industrial City ,Dubai"},{"place_name":"Al Mankhool ,Bur Dubai"},{"place_name":"Muraqqabat ,Deira"},{"place_name":"NSHAMA Town Square ,Bawadi"},{"place_name":"Al Barsha 2 ,Al Barsha"},{"place_name":"Al Mizhar ,Dubai"},{"place_name":"Green Community ,Dubai"},{"place_name":"Dubai Studio City ,Dubai Land"},{"place_name":"Deira ,Dubai"},{"place_name":"Al Warqaa ,Dubai"},{"place_name":"Southridge/Burj Views ,Downtown Dubai"},{"place_name":"Oud Al Muteena ,Dubai"},{"place_name":"Al Qusais ,Dubai"},{"place_name":"Al Barsha 3 ,Al Barsha"},{"place_name":"Al Jadaf ,Dubai"},{"place_name":"Ritaj ,Dubai Investment Park"},{"place_name":"Karama ,Dubai"},{"place_name":"Dubai Sustainable City ,Dubai Land"},{"place_name":"Down Town Jebel Ali ,Jebel Ali"},{"place_name":"IMPZ International Media Production Zone ,Dubai Land"},{"place_name":"Zulal ,The Lakes"},{"place_name":"Creek Golf & Yacht Club ,Deira"},{"place_name":"Liwan - Queue Point ,Dubai Land"},{"place_name":"Akoya Oxygen ,Dubai Land"},{"place_name":"Deema ,The Lakes"},{"place_name":"Jumeirah Heights ,Dubai"},{"place_name":"Remraam ,Dubai Land"},{"place_name":"Motor City ,Dubai Land"},{"place_name":"Forat ,The Lakes"},{"place_name":"Hattan 2 ,The Lakes"},{"place_name":"Green Community Motor City ,Motor City"},{"place_name":"The Royal Estates ,Dubai"},{"place_name":"Maeen ,The Lakes"},{"place_name":"Al Nahda ,Dubai"},{"place_name":"Hattan 3 ,The Lakes"},{"place_name":"Ghadeer Community ,The Lakes"},{"place_name":"Jebel Ali ,Dubai"},{"place_name":"Jumeirah ,Dubai"},{"place_name":"Nad Al Shiba ,Dubai"},{"place_name":"Acacia Avenues ,Al Sufouh"},{"place_name":"Majan ,Dubai"},{"place_name":"DIFC ,Sheikh Zayed Road"},{"place_name":"Ras Al Khor ,Dubai"},{"place_name":"Jumeirah Village ,Dubai"},{"place_name":"The Lagoons ,Dubai"},{"place_name":"DMC, DIC & KV Freezones ,Dubai"},{"place_name":"Garhoud ,Dubai"},{"place_name":"Al Waha ,Dubai Land"},{"place_name":"Al Warsan 2 ,Al Warsan"},{"place_name":"Oud Metha ,Dubai"},{"place_name":"Dubai World Central ,Dubai"},{"place_name":"Golf City ,Dubai Land"},{"place_name":"Al Mamzar ,Al Mamzar"},{"place_name":"Legends Dubai ,Dubai Land"},{"place_name":"Living Legends ,Legends Dubai"},{"place_name":"Muhaisnah ,Dubai"},{"place_name":"Naif ,Deira"},{"place_name":"Academic City ,Dubai"},{"place_name":"Dubiotech ,Dubai"},{"place_name":"City Walk ,Al Wasl"},{"place_name":"Jebel Ali Village ,Jebel Ali"},{"place_name":"The Gardens ,Jebel Ali Village"},{"place_name":"Zabeel ,Dubai"},{"place_name":"Al Quoz Industrial Area 1 ,Al Quoz"},{"place_name":"Al Safa ,Dubai"},{"place_name":"Al Manara ,Dubai"},{"place_name":"Al Safa 2 ,Al Safa"},{"place_name":"NSHAMA Town Square ,Dubai Land"},{"place_name":"Al Khail Heights ,Al Quoz"},{"place_name":"Jumeirah Golf Estates ,Dubai Land"},{"place_name":"Dubai Lifestyle City ,Dubai Land"},{"place_name":"The Plantation, Equestrian & Polo Club ,Dubai Land"},{"place_name":"Nad Al Hamar ,Dubai"},{"place_name":"Trade Centre ,Zabeel"},{"place_name":"Umm Al Sheif ,Dubai"},{"place_name":"Satwa ,Dubai"},{"place_name":"Abu Hail ,Deira"},{"place_name":"Majan ,Dubai Land"},{"place_name":"Bawadi ,Dubai Land"},{"place_name":"Whispering Pines ,Jumeirah Golf Estates"},{"place_name":"Al Quoz 2 ,Al Quoz"},{"place_name":"Dubai ,UAE"},{"place_name":"Hor Al Anz ,Dubai"},{"place_name":"Jumeirah Islands ,Emirates Living"},{"place_name":"Wadi Almardi ,Dubai"},{"place_name":"Rashidiya ,Dubai"},{"place_name":"International City Phase-2 ,International City"},{"place_name":"DMC, DIC & KV Freezones ,Al Sufouh"},{"place_name":"Al Jafiliya ,Dubai"},{"place_name":"Al Barsha South 3 ,Al Barsha"},{"place_name":"Hamriya Port ,Deira"},{"place_name":"Knowledge Village ,DMC, DIC & KV Freezones"},{"place_name":"Jebel Ali Industrial Area ,Jebel Ali"},{"place_name":"Dubai Internet City ,DMC, DIC & KV Freezones"},{"place_name":"Al Majaz ,Sharjah"},{"place_name":"Al Barsha South 2 ,Al Barsha"},{"place_name":"Desert Palm ,Al Warsan 2"}],"status":"true"}

My android code in async background task the code below, I created the JSONArray location;

 try {
            String success = json.getString("status");
            if (success.equals("true")) {
                Log.d("status",":"+success);
                 location = json.getJSONArray("location");
                //location = json.getString("location");
                Log.d("status",":"+location);
            } else {
                // failed to create product
                // flag = true;
            }
            // Simulate network access.
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            return false;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

in post execution the below code

 List<String> data = new ArrayList<>();

        int len = location.length();
        Log.d("len",":"+len);
        for(int i=0; i<len; i++) {

            Log.d(location.getJSONObject("place_name"));
        }

I can't parse the json to array object.

1
  • You should perhaps consider using gson library (github.com/google/gson) to convert json in to a POJO Commented Dec 2, 2016 at 13:49

2 Answers 2

1

in here location is a JsonArray object. so use an integer variable for access data from it :)

ArrayList<String> data=new ArrayList<String>();
for(int i=0;i<location.length();i++){
    JSONObject arrayElement=location.getJSONObject(i);
    data.add(arrayElement.getString("place_name"));
    Log.i("json",data.get(i));
    }
Sign up to request clarification or add additional context in comments.

Comments

0

change Log.d(location.getJSONObject("place_name"));

to

Log.d(location.getJSONObject(i).getString("place_name");

2 Comments

Log.d(location.getJSONObject(i).getString("place_name")); shows unhandels exception error
you have to add this line in try - catch block with json exception. Aren't you using Android studio?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.