Android: 如何将Volley 中的json 数据设置到ListView 中?
Android: How to set the json data from Volley into the ListView?
我最近开始使用 volley,我遇到了一些我觉得很好奇的问题。我有一组要在列表视图中设置的 json 数据。
{"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 数据的值显示到列表视图中,
所以我这样试了。
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=值}
实际上,我只想显示值。
价值
尝试使用下面的示例。
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;
}
我最近开始使用 volley,我遇到了一些我觉得很好奇的问题。我有一组要在列表视图中设置的 json 数据。
{"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 数据的值显示到列表视图中, 所以我这样试了。
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=值}
实际上,我只想显示值。
价值
尝试使用下面的示例。
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;
}