/** * Get the value object associated with a key (from an enum name()). * * @param key * A key string. * @return The object associated with the key. * @throws JSONException * if the key is not found. */ public Object get(Enum<?> key) throws JSONException { return get(key.name()); }
/** * Get the string associated with a key. * * @param key * A key string. * @return A string which is the value. * @throws JSONException * if the key is not found. */ public String getString(String key) throws JSONException { return get(key).toString(); }
/** * Get the value object associated with a key (from an enum name()). * * @param key * A key string. * @return The object associated with the key. * @throws JSONException * if the key is not found. */ public Object get(Enum<?> key) throws JSONException { return get(key.name()); }
/** * Get the string associated with a key. * * @param key * A key string. * @return A string which is the value. * @throws JSONException * if the key is not found. */ public String getString(String key) throws JSONException { return get(key).toString(); }
/** * Get the boolean value associated with a key. * * @param key * A key string. * @return The truth. * @throws JSONException * if the value is not a Boolean or the String "true" or "false". */ public boolean getBoolean(String key) throws JSONException { Object o = get(key); if (o.equals(Boolean.FALSE) || (o instanceof String && ((String) o).equalsIgnoreCase("false"))) { return false; } else if (o.equals(Boolean.TRUE) || (o instanceof String && ((String) o).equalsIgnoreCase("true"))) { return true; } throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean."); }
/** * Get the long value associated with a key. * * @param key * A key string. * @return The long value. * @throws JSONException * if the key is not found or if the value cannot be converted to a * long. */ public long getLong(String key) throws JSONException { Object o = get(key); try { return o instanceof Number ? ((Number) o).longValue() : Long.parseLong((String) o); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a long."); } }
/** * Get the boolean value associated with a key. * * @param key * A key string. * @return The truth. * @throws JSONException * if the value is not a Boolean or the String "true" or "false". */ public boolean getBoolean(String key) throws JSONException { Object o = get(key); if (o.equals(Boolean.FALSE) || (o instanceof String && ((String) o).equalsIgnoreCase("false"))) { return false; } else if (o.equals(Boolean.TRUE) || (o instanceof String && ((String) o).equalsIgnoreCase("true"))) { return true; } throw new JSONException("JSONObject[" + quote(key) + "] is not a Boolean."); }
/** * Get the double value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number object * and cannot be converted to a number. */ public double getDouble(String key) throws JSONException { Object o = get(key); try { return o instanceof Number ? ((Number) o).doubleValue() : Double.valueOf((String) o) .doubleValue(); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); } }
/** * Get the int value associated with a key. * * @param key * A key string. * @return The integer value. * @throws JSONException * if the key is not found or if the value cannot be converted to an * integer. */ public int getInt(String key) throws JSONException { Object o = get(key); try { return o instanceof Number ? ((Number) o).intValue() : Integer.parseInt((String) o); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not an int."); } }
/** * Get the double value associated with a key. * * @param key * A key string. * @return The numeric value. * @throws JSONException * if the key is not found or if the value is not a Number object * and cannot be converted to a number. */ public double getDouble(String key) throws JSONException { Object o = get(key); try { return o instanceof Number ? ((Number) o).doubleValue() : Double.valueOf((String) o) .doubleValue(); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); } }
/** * Get the int value associated with a key. * * @param key * A key string. * @return The integer value. * @throws JSONException * if the key is not found or if the value cannot be converted to an * integer. */ public int getInt(String key) throws JSONException { Object o = get(key); try { return o instanceof Number ? ((Number) o).intValue() : Integer.parseInt((String) o); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not an int."); } }
/** * Get the long value associated with a key. * * @param key * A key string. * @return The long value. * @throws JSONException * if the key is not found or if the value cannot be converted to a * long. */ public long getLong(String key) throws JSONException { Object o = get(key); try { return o instanceof Number ? ((Number) o).longValue() : Long.parseLong((String) o); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a long."); } }
/** * Get the JSONArray value associated with a key. * * @param key * A key string. * @return A JSONArray which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONArray. */ public JSONArray getJSONArray(String key) throws JSONException { Object o = get(key); if (o instanceof JSONArray) { return (JSONArray) o; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Get the JSONObject value associated with a key. * * @param key * A key string. * @return A JSONObject which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONObject. */ public JSONObject getJSONObject(String key) throws JSONException { Object o = get(key); if (o instanceof JSONObject) { return (JSONObject) o; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
/** * Get the JSONArray value associated with a key. * * @param key * A key string. * @return A JSONArray which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONArray. */ public JSONArray getJSONArray(String key) throws JSONException { Object o = get(key); if (o instanceof JSONArray) { return (JSONArray) o; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); }
/** * Get the JSONObject value associated with a key. * * @param key * A key string. * @return A JSONObject which is the value. * @throws JSONException * if the key is not found or if the value is not a JSONObject. */ public JSONObject getJSONObject(String key) throws JSONException { Object o = get(key); if (o instanceof JSONObject) { return (JSONObject) o; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); }
@Override public List<IMetadata> search(String query, String authorityId) { ArrayList<IMetadata> ret = new ArrayList<IMetadata>(); try { JSONResource res = new Resty().json(getSearchURL(query)); JSONArray ares = res.array(); // JSONArray ares = (JSONArray) res.get("results"); for (int i = 0; i < ares.length(); i++) { JSONObject ores = (JSONObject) ares.get(i); if (ores != null) { Object rank = ores.get("rank"); if (!rankOK(rank)) continue; Metadata md = new Metadata(); md.put("ID", ores.get("speciesKey")); md.put("Label", ores.get("canonicalName")); ret.add(md); } } } catch (Exception e) { } return ret; }
/** * TODO: * * 1. use specific authority 2. use Java clients from GBIF 3. provide paging * * @param query * @return */ public List<IMetadata> searchGBIF(String query) { ArrayList<IMetadata> ret = new ArrayList<>(); try { JSONResource res = new Resty().json(getSearchURL(query)); JSONArray ares = res.array(); // JSONArray ares = (JSONArray) res.get("results"); for (int i = 0; i < ares.length(); i++) { JSONObject ores = (JSONObject) ares.get(i); if (ores != null) { Object rank = ores.get("rank"); if (!rankOK(rank)) continue; Metadata md = new Metadata(); md.put("ID", ores.get("speciesKey")); md.put("Label", ores.get("canonicalName")); ret.add(md); } } } catch (Exception e) { } return ret; }
} else if (o instanceof JSONArray) JSONArray array = JSONArray.class.cast(o); // return all items List<Object > matches = new ArrayList<Object >(); Object result = null; return matches;
} else if (o instanceof JSONArray) JSONArray array = JSONArray.class.cast(o); // return all items List<Object > matches = new ArrayList<Object >(); Object result = null; return matches;