private Map jsonResult(String data) {
ArrayList<HashMap<String, String>> mList= new ArrayList<HashMap<String, String>>();
try {
hmap = gson.fromJson(json, type);
mList.add(hmap); // 将数据添加
}
} catch (JSONException e) {
e.printStackTrace();
}
return mList;
}
ArrayList<HashMap<String, String>> mList= new ArrayList<HashMap<String, String>>();
try {
JSONArray array; //json数组
JSONObject job = new JSONObject(data);
array = job.getJSONArray("result"); //得到对应的resultfor (int i = 0; i < array.length(); i++) {
HashMap<String, String> hmap = new HashMap<String, String>();
String json = array.getString(i);
GsonBuilder builder = new GsonBuilder();Gson gson = builder.create()
// 得到的数据类型。
Type type = new TypeToken<HashMap<String, String>>() {}.getType();hmap = gson.fromJson(json, type);
mList.add(hmap); // 将数据添加
}
} catch (JSONException e) {
e.printStackTrace();
}
return mList;
}
本文介绍了一种使用Java从JSON字符串解析并转换为HashMap列表的方法。通过Gson库将JSON数组中的每个元素转换成HashMap对象,并收集到ArrayList中返回。文章详细展示了如何处理JSON数据,包括异常捕获。
22万+

被折叠的 条评论
为什么被折叠?



