2015-02-07 111 views
0

我最近開始使用凌空,我有一些問題,我覺得好奇。我有一組json數據,我想在列表視圖中設置。Android:如何將來自Volley的json數據設置爲ListView?

{"classification": 
    [ 
    {"1":"\u30b8\u30e3\u30f3\u30dc\u5b9d\u304f\u3058"}, 
    {"2":"\u95a2\u6771\u30fb\u4e2d\u90e8\u30fb\u6771\u5317\u81ea\u6cbb\u5b9d\u304f\u3058"}, 
    {"3":"\u8fd1\u757f\u5b9d\u304f\u3058"}, 
    {"4":"\u897f\u65e5\u672c\u5b9d\u304f\u3058"} 
    ], 
     "result":"OK"} 

我想顯示從JSON數據的值以上到ListView中, 所以我試圖像這樣。

 JSONObject jsonObject = new JSONObject(response.toString()); 
       JSONArray js = jsonObject.getJSONArray("classification"); 
       List ll = getListFromJsonArray(js); 
       lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), 
         android.R.layout.simple_list_item_1, ll)); 

不管怎樣,輸出並不像我預期的那樣。它顯示這樣的東西。

{1 = value}

其實,我只想顯示值。

value

+0

郵政'getListFromJsonArray'方法代碼 – 2015-02-07 14:45:57

回答

2

嘗試使用下面的示例。

try { 
         JSONObject jsonObject = new JSONObject(response.toString()); 
         JSONArray js = jsonObject.names(); 
         JSONArray val = jsonObject.toJSONArray(js); 
         List ll = getListFromJsonArray(val); 
         lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(), 
           android.R.layout.simple_list_item_1, ll)); 

        }catch(Exception e){ 

        } 

///定製方法

// method converts JSONArray to List of Maps 
    protected static List<Map<String, String>> getListFromJsonArray(JSONArray jsonArray) { 
     ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); 
     Map<String, String> map; 
     // fill the list 
     for (int i = 0; i < jsonArray.length(); i++) { 
      map = new HashMap<String, String>(); 
      try { 
       JSONObject jo = (JSONObject) jsonArray.get(i); 
       // fill map 
       Iterator iter = jo.keys(); 
       while(iter.hasNext()) { 
        String currentKey = (String) iter.next(); 
        map.put(currentKey, jo.getString(currentKey)); 
       } 
       // add map to list 
       list.add(map); 
      } catch (JSONException e) { 
       Log.e("JSON", e.getLocalizedMessage()); 
      } 


     } 
     return list; 
    } 
相關問題