private Event castJSONObjToEvent(JSONObject jsonObj) { Event event = new Event(); DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH); try { String startTime = jsonObj.get("startTime").toString(); String endTime = jsonObj.get("endTime").toString(); event.setName(jsonObj.getString("name")); event.setDescription(jsonObj.getString("description")); event.setCategory(getCategory(jsonObj)); event.setStatus(getStatus(jsonObj)); event.setLocation(jsonObj.getString("location")); if (event.getCategory().equals(GenericEvent.Category.FLOATING)) { // doesn't set start time and end time } else { event.setStartTime(df.parse(startTime)); event.setEndTime(df.parse(endTime)); } } catch (JSONException e2) { e2.printStackTrace(); } catch (ParseException e1) { e1.printStackTrace(); } return event; }
private GenericEvent.Category getCategory(JSONObject jsonObj) throws JSONException { if (jsonObj.get("category").equals(Constant.CATEGORY_DEADLINE)) { return GenericEvent.Category.DEADLINE; } else if (jsonObj.get("category").equals(Constant.CATEGORY_EVENT)) { return GenericEvent.Category.EVENT; } else { return GenericEvent.Category.FLOATING; } }
private GenericEvent.Status getStatus(JSONObject jsonObj) throws JSONException { if (jsonObj.get("status").equals("COMPLETE")) { return GenericEvent.Status.COMPLETE; } else if (jsonObj.get("status").equals("INCOMPLETE")) { return GenericEvent.Status.INCOMPLETE; } else if (jsonObj.get("status").equals("UNDETERMINED")) { return GenericEvent.Status.UNDETERMINED; } else { return GenericEvent.Status.NULL; } }
public void parse(String displayParameters) throws JSONException { jsonObject = new JSONObject(displayParameters).getJSONObject("hmd"); JSONArray jsonEyes = (JSONArray) jsonObject.get("eyes"); eyes = new Eye[jsonEyes.length()]; for (int i = 0; i < jsonEyes.length(); i++) { eyes[i] = new Eye(jsonEyes.getJSONObject(i)); } JSONArray jsonRes = (JSONArray) jsonObject.get("resolutions"); resolutions = new Resolution[jsonRes.length()]; for (int i = 0; i < jsonRes.length(); i++) { resolutions[i] = new Resolution(jsonRes.getJSONObject(i)); } }
private ReservedEvent castJSONObjToReservedEvent(JSONObject jsonObj) { ReservedEvent event = new ReservedEvent(); DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH); int max = 100; int count = 0; try { event.setName(jsonObj.getString("name")); event.setDescription(jsonObj.getString("description")); event.setCategory(getCategory(jsonObj)); event.setStatus(getStatus(jsonObj)); event.setLocation(jsonObj.getString("location")); for (int i = 0; i < 100; i++) { try { jsonObj.getString("startTime" + i); } catch (JSONException e) { count = i; break; } } ArrayList<TimePair> reservedTimes = new ArrayList<TimePair>(); for (int i = 0; i < count; i++) { TimePair t = new TimePair( df.parse(jsonObj.get("startTime" + i).toString()), df.parse(jsonObj.get("endTime" + i).toString())); reservedTimes.add(t); } event.setReservedTimes(reservedTimes); } catch (JSONException e2) { e2.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return event; }
public Object get(String key) throws JSONException { return jsonObject.get(key); }