/** * Get the int value associated with an index. * * @param index The index must be between 0 and length() - 1. * @return The value. * @throws JsonException If the key is not found or if the value cannot be converted to a * number. if the value cannot be converted to a number. */ public int getInt(int index) throws JsonException { Object o = get(index); return o instanceof Number ? ((Number) o).intValue() : (int) getDouble(index); }
/** * Get the string associated with an index. * * @param index The index must be between 0 and length() - 1. * @return A string value. * @throws JsonException If there is no value for the index. */ public String getString(int index) throws JsonException { return get(index).toString(); }
/** * Get the JsonArray associated with an index. * * @param index The index must be between 0 and length() - 1. * @return A JsonArray value. * @throws JsonException If there is no value for the index. or if the value is not a * JsonArray */ public JsonArray getJsonArray(int index) throws JsonException { Object o = get(index); if (o instanceof JsonArray) { return (JsonArray) o; } throw new JsonException("JsonArray[" + index + "] is not a JsonArray."); }
/** * Get the long value associated with an index. * * @param index The index must be between 0 and length() - 1. * @return The value. * @throws JsonException If the key is not found or if the value cannot be converted to a * number. */ public long getLong(int index) throws JsonException { Object o = get(index); return o instanceof Number ? ((Number) o).longValue() : (long) getDouble(index); }
/** * Get the JsonObject associated with an index. * * @param index subscript * @return A JsonObject value. * @throws JsonException If there is no value for the index or if the value is not a * JsonObject */ public JsonObject getJsonObject(int index) throws JsonException { Object o = get(index); if (o instanceof JsonObject) { return (JsonObject) o; } throw new JsonException("JsonArray[" + index + "] is not a JsonObject."); }
/** * Get the double value associated with an index. * * @param index The index must be between 0 and length() - 1. * @return The value. * @throws JsonException If the key is not found or if the value cannot be converted to a * number. */ public double getDouble(int index) throws JsonException { Object o = get(index); try { return o instanceof Number ? ((Number) o).doubleValue() : Double.valueOf((String) o).doubleValue(); } catch (Exception e) { throw new JsonException("JsonArray[" + index + "] is not a number."); } }
/** * Get the boolean value associated with an index. The string values "true" * and "false" are converted to boolean. * * @param index The index must be between 0 and length() - 1. * @return The truth. * @throws JsonException If there is no value for the index or if the value is not * convertable to boolean. */ public boolean getBoolean(int index) throws JsonException { Object o = get(index); 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("JsonArray[" + index + "] is not a Boolean."); }
private AdsImageCropsSpec createSpecFromString(String input) { AdsImageCropsSpec spec = null; if (input != null) { spec = new AdsImageCropsSpec(); JsonValue ar = Json.parse(input); JsonArray outerArray = ar.asArray(); JsonArray leftTop = outerArray.get(0).asArray(); spec.setLeft(leftTop.get(0).asLong()); spec.setTop(leftTop.get(1).asLong()); JsonArray rightBottom = outerArray.get(1).asArray(); spec.setRight(rightBottom.get(0).asLong()); spec.setBottom(rightBottom.get(1).asLong()); } return spec; }
@SuppressWarnings("unchecked") protected <T> Connection<T> mapToConnection(String connectionJson, Class<T> connectionType) { List<T> data = new ArrayList<T>(); String previous = null; String next = null; try { JsonObject jsonObject = new JsonObject(connectionJson); // Pull out data JsonArray jsonData = jsonObject.getJsonArray("data"); for (int i = 0; i < jsonData.length(); i++) data.add(connectionType.equals(JsonObject.class) ? (T) jsonData.get(i) : jsonMapper.toJavaObject(jsonData .get(i).toString(), connectionType)); // Pull out paging info, if present if (jsonObject.has("paging")) { JsonObject jsonPaging = jsonObject.getJsonObject("paging"); previous = jsonPaging.has("previous") ? jsonPaging.getString("previous") : null; next = jsonPaging.has("next") ? jsonPaging.getString("next") : null; } } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to map connection JSON to Java objects", e); } return new Connection<T>(data, previous, next); }
list.add(toJavaObject(jsonArray.get(i).toString(), type));