Json到数组列表转换

时间:2016-12-02 13:40:02

标签: android json

下面是我的json代码,我无法将其转换为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"}

我的android代码在异步后台任务下面的代码, 我创建了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();
    }

在后期执行下面的代码

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

我无法将json解析为数组对象。

2 个答案:

答案 0 :(得分:1)

这里的位置是一个JsonArray对象。所以使用整数变量来访问它的数据:)

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

答案 1 :(得分:0)

更改Log.d(location.getJSONObject("place_name"));

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