简单易用的JSON与List相互转换
用系统自带包:org.json就可以
List集合封装了object,下面是list到json
- /**
- *
- * @param list
- * 存放书签的集合
- * @return json格式对象
- */
- private static JSONObject listTojsoJsonObject(List<CalendarEvent> list) {
- JSONObject jsonObj = new JSONObject();
- try {
- jsonObj.put("calendarEvents", list);
- } catch (JSONException e) {
- // The JSONException is thrown by the JSON.org classes when things
- // are amiss.
- e.printStackTrace();
- }
- return jsonObj;
- }
下边是json到list
- **
- *
- * @param json
- * @return list
- * @throws JSONException
- */
- public static List<CalendarEvent> jsonTolist(JSONObject json)
- throws JSONException {
- List<CalendarEvent> list = (List) json.get("calendarEvents");
- return list;
- }