List of usage examples for org.json JSONObject get
public Object get(String key) throws JSONException
From source file:co.appvigil.requestHandler.AccessTokenRequest.java
public Response requestNewAccessToken(String... requestString) throws JSONException, ImproperKeyException, ClientProtocolException, IOException { if (requestString.length > 5) { throw new ImproperKeyException(); } else {/*from ww w .j a va 2 s . c o m*/ if (validate(requestString)) { List<NameValuePair> params = getParams(requestString); params.add(new BasicNameValuePair("api_key", apiKey)); params.add(new BasicNameValuePair("api_secret", apiSecret)); JSONObject jObj = connection.get(Constants.ACCESS_TOKEN_NEW, params); JSONObject response = null, meta; int code = 0; String message = null; Response responseData = new Response(); response = (JSONObject) jObj.get("response"); meta = (JSONObject) jObj.get("meta"); code = meta.getInt("code"); message = (String) response.get("message"); responseData.setCode(code); responseData.setMessage(message); if (code == 200) { AccessToken accessToken = new AccessToken(); accessToken.setAccessTokenString(response.getString("access_token")); accessToken.setTimetoLive(response.getInt("ttl_in_seconds")); responseData.setRequestModel(accessToken); } return responseData; } else { throw new ImproperKeyException(); } } }
From source file:co.appvigil.requestHandler.AccessTokenRequest.java
public Response renewAccessToken(AccessToken accessToken, String... timeToLive) throws JSONException, AccessTokenRequestException, ClientProtocolException, IOException { if (accessToken != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("access_token", accessToken.getAccessTokenString())); if (timeToLive.length == 1) { String key = timeToLive[0].split("=")[0]; if (key == "new_ttl") params.add(new BasicNameValuePair(key, timeToLive[0].split("=")[1])); }/* www.j av a 2s . c o m*/ JSONObject jObj = connection.get(Constants.ACCESS_TOKEN_RENEW, params); JSONObject response = null, meta; int code = 0; String message = null; Response responseData = new Response(); response = (JSONObject) jObj.get("response"); meta = (JSONObject) jObj.get("meta"); code = meta.getInt("code"); message = (String) response.get("message"); responseData.setCode(code); responseData.setMessage(message); if (code == 201) { accessToken.setAccessTokenString(response.getString("access_token")); accessToken.setTimetoLive(response.getInt("ttl_in_seconds")); responseData.setRequestModel(accessToken); } return responseData; } else { throw new AccessTokenRequestException(); } }
From source file:co.appvigil.requestHandler.AccessTokenRequest.java
public Response ViewAccessToken(AccessToken accessToken) throws JSONException, AccessTokenRequestException, ClientProtocolException, IOException { if (accessToken != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("access_token", accessToken.getAccessTokenString())); JSONObject jObj = connection.get(Constants.ACCESS_TOKEN_VIEW, params); JSONObject response = null, meta; int code = 0; String message = null;/* w ww .j a va2 s .c o m*/ Response responseData = new Response(); response = (JSONObject) jObj.get("response"); meta = (JSONObject) jObj.get("meta"); code = meta.getInt("code"); message = (String) response.get("message"); responseData.setCode(code); responseData.setMessage(message); if (code == 200) { accessToken.setAccessTokenString(response.getString("access_token")); accessToken.setTimetoLive(response.getInt("ttl_in_seconds")); accessToken.setIssueDateTime(response.getString("issue_date_time")); responseData.setRequestModel(accessToken); } return responseData; } else { throw new AccessTokenRequestException(); } }
From source file:co.appvigil.requestHandler.AccessTokenRequest.java
public Response flushAccessToken(AccessToken accessToken) throws JSONException, AccessTokenRequestException, ClientProtocolException, IOException { if (accessToken != null) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("access_token", accessToken.getAccessTokenString())); JSONObject jObj = connection.get(Constants.ACCESS_TOKEN_FLUSH, params); JSONObject response = null, meta; int code = 0; String message = null;/*from w w w . j a v a 2 s. c o m*/ Response responseData = new Response(); response = (JSONObject) jObj.get("response"); meta = (JSONObject) jObj.get("meta"); code = meta.getInt("code"); message = (String) response.get("message"); responseData.setCode(code); responseData.setMessage(message); if (code == 202) { responseData.setRequestModel(null); } return responseData; } else { throw new AccessTokenRequestException(); } }
From source file:ru.kaefik.isaifutdinov.an_weather_widget.utils.Utils.java
static public String getObjFromJson(String sjosn, String nameParrent, String nameChild) { JSONObject parentObject = null; JSONObject childObject = null;/*from w w w .j a v a2s . c om*/ String res = null; try { parentObject = new JSONObject(sjosn); if (nameParrent != null) { res = parentObject.get(nameParrent).toString(); if (nameChild != null) { if (res != null) { childObject = new JSONObject(res); res = childObject.get(nameChild).toString(); } } } } catch (JSONException e) { res = "0"; } return res; }
From source file:org.protorabbit.json.JSONUtil.java
@SuppressWarnings("unchecked") public static Object cloneJSON(Object target) { if (target == null) return JSONObject.NULL; if (target instanceof JSONObject) { Object o = null;//from w ww . j a va 2 s . com o = new JSONObject(); JSONObject jo = (JSONObject) target; Iterator<String> it = jo.keys(); while (it.hasNext()) { String key = it.next(); try { ((JSONObject) o).put(key, cloneJSON(jo.get(key))); } catch (JSONException e) { e.printStackTrace(); } } return o; } else if (target instanceof JSONArray) { Object o = new JSONArray(); JSONArray ja = (JSONArray) target; int len = ja.length(); for (int i = 0; i < len; i++) { try { ((JSONArray) o).put(cloneJSON(ja.get(i))); } catch (JSONException e) { e.printStackTrace(); } } } else if (target instanceof Long) { return new Long(((Long) target).longValue()); } else if (target instanceof Double) { return new Double(((Double) target).doubleValue()); } else if (target instanceof Integer) { return new Integer(((Integer) target).intValue()); } else if (target instanceof Boolean) { return new Boolean(((Boolean) target).booleanValue()); } return target; }
From source file:org.protorabbit.json.JSONUtil.java
@SuppressWarnings("unchecked") public static void mixin(JSONObject child, JSONObject parent, String[] skip) { Iterator<String> it = parent.keys(); while (it.hasNext()) { String key = it.next();/* w w w.j a v a 2 s . co m*/ // don't mix in the skip for (int i = 0; i < skip.length; i++) { if (key.equals(skip[i])) { continue; } } if (child.has(key)) { continue; } else { Object o; try { o = parent.get(key); child.put(key, cloneJSON(o)); } catch (JSONException e) { e.printStackTrace(); } } } }
From source file:com.marketcloud.marketcloud.Json.java
/** * Parses a JSONObject, looking for the given structure. <br /> * <br />/*from ww w. java2s . c o m*/ * Suppose you have a json like <br /> * { <br /> * "id" : 1234, <br /> * "name" : "abcd", <br /> * "image_url" : "http://example" <br /> * } <br /> * and you want to parse its data. If you don't want to do it manually, and you already know its structure, * you can use this method. Just pass a string array containing the keys {"id", "name", "image_url"} and * the method will parse the data for you. <br /> * <br /> * Note: if the structure is not correct, the method will throw an exception and fail. If the JSONObject * contains a JSONArray, only the first argument of the array (the first object) will be parsed: if you * need to use this method for a list, please call it once for every list item. * * @param keys json fields names * @return an Hashmap with the parsed data */ @SuppressWarnings("unused") public HashMap<String, Object> parseData(String[] keys, JSONObject jsonObject) throws JSONException { HashMap<String, Object> map = new HashMap<>(); if (jsonObject.has("data")) jsonObject = getData(jsonObject)[0]; for (String key : keys) { map.put(key, jsonObject.get(key)); } return map; }
From source file:com.marketcloud.marketcloud.Json.java
/** * Parses a JSONObject, looking for the given structure. <br /> * <br />//w w w.j a v a2s . c om * It differs from the other parseData because the user should provide a pre-prepared Hashmap that will be * filled by the method. The keys of the map will be used as the string array of the other parseData. * <br /> * In order to avoid override of data that you need to keep, and to use the method without getting an * exception, you could pass an "ignore" list containing the keys that you want the method to ignore, * simply adding all the keys you want to ignore to the method call. <br /> * Example: parseData(map, jsonObject, "price", "source"); <br /> * If the map contains the keys "price" and "source", then the method will ignore them. <br /> * The normal method call remains: parseData(map, jsonObject) . * <br /> * Note: if the structure is not correct, the method will throw an exception and fail. If the JSONObject * contains a JSONArray, only the first argument of the array (the first object) will be parsed. * * @param map pre-prepared Hashmap * @return an Hashmap with the parsed data */ @SuppressWarnings("unused") public HashMap<String, Object> parseData(HashMap<String, Object> map, JSONObject jsonObject, String... ignore) throws JSONException { if (jsonObject.has("data")) jsonObject = getData(jsonObject)[0]; ArrayList<String> ignorelist = new ArrayList<>(); Collections.addAll(ignorelist, ignore); for (String key : map.keySet()) { if (!ignorelist.contains(key)) map.put(key, jsonObject.get(key)); } return map; }
From source file:com.marketcloud.marketcloud.Json.java
/** * Parses a JSONObject, looking for the given structure. <br /> * <br />// w ww . j a v a 2s . c om * It differs from the other parseData because the user should provide a pre-prepared Hashmap that will be * filled by the method. The keys of the map will be used as the string array of the other parseData. * <br /> * In order to avoid override of data that you need to keep, and to use the method without getting an * exception, you could pass an "ignore" list containing the keys that you want the method to ignore. * <br /> * Note: if the structure is not correct, the method will throw an exception and fail. If the JSONObject * contains a JSONArray, only the first argument of the array (the first object) will be parsed. * * @param map pre-prepared Hashmap * @return an Hashmap with the parsed data */ @SuppressWarnings("unused") public HashMap<String, Object> parseData(HashMap<String, Object> map, JSONObject jsonObject, List<String> ignore) throws JSONException { if (jsonObject.has("data")) jsonObject = getData(jsonObject)[0]; for (String key : map.keySet()) { if (!ignore.contains(key)) map.put(key, jsonObject.get(key)); } return map; }