* Get a set of categories from a JSONObject * @param event * @return A set of string * @throws JSONException */ private Set<String> getCategories(JSONObject event)throws JSONException { Set<String> categories = new HashSet<>(); if (!event.isNull("classifications")) { JSONArray classifications = event.getJSONArray("classifications"); for (int i = 0; i < classifications.length(); i++) { JSONObject classification = classifications.getJSONObject(i); if (!classification.isNull("segment")) { JSONObject segment = classification.getJSONObject("segment"); if (!segment.isNull("name")) { String name = segment.getString("name"); categories.add(name); } } } }
* Convert JSONArray to a list of item objects * @param events * @return A list of Item objects * @throws JSONException */ private List<Item> getItemList(JSONArray events)throws JSONException { List<Item> itemList = new ArrayList<>();
for (int i = 0; i < events.length(); ++i) { JSONObject event = events.getJSONObject(i); ItemBuilder builder = new ItemBuilder(); if (!event.isNull("name")) { builder.setName(event.getString("name")); } if (!event.isNull("id")) { builder.setItemId(event.getString("id")); } if (!event.isNull("url")) { builder.setUrl(event.getString("url")); } if (!event.isNull("rating")) { builder.setRating(event.getDouble("rating")); } if (!event.isNull("distance")) { builder.setDistance(event.getDouble("distance")); } builder.setCategories(getCategories(event)); builder.setAddress(getAddress(event)); builder.setImageUrl(getImageUrl(event)); itemList.add(builder.build()); }
近期评论