Android:如何将来自Volley的json数据设置为ListView?

问题描述:

我最近开始使用凌空,我有一些问题,我觉得好奇。我有一组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

尝试使用下面的示例。

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