Example usage for org.json.simple JSONObject get

List of usage examples for org.json.simple JSONObject get

Introduction

In this page you can find the example usage for org.json.simple JSONObject get.

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:hoot.services.controllers.job.JobControllerBase.java

static String getParameterValue(String key, JSONArray args) {
    for (Object arg : args) {
        JSONObject o = (JSONObject) arg;
        if (o.containsKey(key)) {
            return o.get(key).toString();
        }/*  ww w  .  j av a  2 s  .  c om*/
    }

    return null;
}

From source file:com.storageroomapp.client.util.JsonSimpleUtil.java

/**
 * Convenience method for pulling a Calendar value off of a JSONObject
 * @param obj the JSONObject received from the server
 * @param key the String key of the value we want
 * @return the Calendar value, or null if not found or not a proper timestamp
 *//* ww  w.  java2  s. c o m*/
static public Calendar parseJsonTimeValue(JSONObject obj, String key) {
    if ((obj == null) || (key == null)) {
        return null;
    }
    Calendar value = null;
    Object valueObj = obj.get(key);
    if (valueObj != null) {
        String valueStr = valueObj.toString();
        value = StorageRoomUtil.storageRoomTimeStringToCalendar(valueStr);
    }
    return value;

}

From source file:com.storageroomapp.client.util.JsonSimpleUtil.java

/**
 * Convenience method for pulling a Calendar value off of a JSONObject
 * @param obj the JSONObject received from the server
 * @param key the String key of the value we want
 * @return the Calendar value, or null if not found or not a proper timestamp
 *//*from ww  w .  java2  s .  c  o m*/
static public Calendar parseJsonDateValue(JSONObject obj, String key) {
    if ((obj == null) || (key == null)) {
        return null;
    }
    Calendar value = null;
    Object valueObj = obj.get(key);
    if (valueObj != null) {
        String valueStr = valueObj.toString();
        value = StorageRoomUtil.storageRoomDateStringToCalendar(valueStr);
    }
    return value;

}

From source file:it.cnr.isti.thematrix.scripting.sys.DatasetSchema.java

/**
 * Convert the argument JSON string into a DatasetSchema
 * @param jsonContent //from   w w  w  . ja v a  2s .  com
 * @return a DatasetSchema
 */
public static DatasetSchema fromJSON(String jsonContent) {
    Object obj = JSONValue.parse(jsonContent);
    JSONArray array = (JSONArray) obj;

    String schemaName = (String) array.get(0);
    DatasetSchema ds = new DatasetSchema(schemaName);

    for (int i = 1; i < array.size(); i++) {
        JSONObject o = (JSONObject) array.get(i);
        String name = (String) o.get("name");
        DataType type = DataType.valueOf((String) o.get("type"));
        ds.put(new Symbol<Integer>(name, null, type));
    }

    return ds;
}

From source file:br.ufrgs.ufrgsmapas.network.LocationParser.java

public static void parseBuildings(SparseArray<BuildingVo> mTempBuilding) {

    // Read JSON File
    Connection con = HttpConnection.connect(URL);
    con.method(Connection.Method.POST).ignoreContentType(true);
    Connection.Response resp;
    try {/* w w  w  .ja  v  a 2  s  .co m*/
        resp = con.execute();
    } catch (IOException e) {
        if (DebugUtils.DEBUG)
            Log.e(TAG, "Error fetching positions: " + e);
        return;
    }
    String jsonDoc = resp.body();

    // Parse JSON to find each element
    JSONObject jsonStartObject = (JSONObject) JSONValue.parse(jsonDoc);

    // Get the array with objects representing each building
    JSONArray jsonBuildings = (JSONArray) jsonStartObject.get("features");

    int buildingsListSize = ((List) jsonBuildings).size();
    // Iterate through buildings
    for (int i = 0; i < buildingsListSize; i++) {
        // Get buildingVo object
        JSONObject jsonBuilding = (JSONObject) jsonBuildings.get(i);

        // Get buildingVo id
        int id = ((Number) jsonBuilding.get("id")).intValue();
        // Get coordinates array
        JSONObject jsonGeometry = (JSONObject) jsonBuilding.get("geometry");
        JSONArray jsonCoordinates = (JSONArray) ((JSONArray) ((JSONArray) jsonGeometry.get("coordinates"))
                .get(0)).get(0);

        // Get latitudes and longitudes
        List coordList = jsonCoordinates;
        int coordListSize = coordList.size();
        double[] latitude = new double[coordListSize];
        double[] longitude = new double[coordListSize];
        for (int j = 0; j < coordListSize; j++) {
            latitude[j] = (double) ((List) coordList.get(j)).get(1);
            longitude[j] = (double) ((List) coordList.get(j)).get(0);
        }

        // Create buildingVo object and add to list - store only the center of the position
        double[] center = centroid(latitude, longitude);
        BuildingVo buildingVo = new BuildingVo(id, center[0], center[1]);
        mTempBuilding.append(id, buildingVo);

    }

}

From source file:com.example.prathik1.drfarm.OCRRestAPI.java

private static void PrintOCRResponse(String jsonResponse) throws ParseException, IOException {
        // Parse JSON data
        JSONParser parser = new JSONParser();
        JSONObject jsonObj = (JSONObject) parser.parse(jsonResponse);

        // Get available pages
        System.out.println("Available pages: " + jsonObj.get("AvailablePages"));

        // get an array from the JSON object
        JSONArray text = (JSONArray) jsonObj.get("OCRText");

        // For zonal OCR: OCRText[z][p]    z - zone, p - pages
        for (int i = 0; i < text.size(); i++) {
            System.out.println(" " + text.get(i));
        }//from w w  w.j av  a2s .  c o m

        // Output file URL
        String outputFileUrl = (String) jsonObj.get("OutputFileUrl");

        // If output file URL is specified
        if (outputFileUrl != null && !outputFileUrl.equals("")) {
            // Download output file
            DownloadConvertedFile(outputFileUrl);
        }
    }

From source file:com.turt2live.hurtle.uuid.UUIDServiceProvider.java

/**
 * Gets the known username history of a UUID. All dates are in UTC.
 * This returns a map of player names and when they were last seen (the
 * approximate date they stopped using that name).
 *
 * @param uuid the uuid to lookup, cannot be null
 *
 * @return a map of names and dates (UTC), or an empty map for invalid input or unknown/non-existent history
 *//* w w w  . j  a v a 2  s . c  o m*/
public static Map<String, Date> getHistory(UUID uuid) {
    if (uuid == null)
        return new HashMap<>();
    try {
        URL url = new URL("http://uuid.turt2live.com/history/" + uuid.toString().replaceAll("-", ""));
        BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
        String parsed = "";
        String line;
        while ((line = reader.readLine()) != null)
            parsed += line;
        reader.close();

        Map<String, Date> map = new HashMap<>();
        Object o = JSONValue.parse(parsed);
        if (o instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) o;
            Object namesObj = jsonObject.get("names");
            if (namesObj instanceof JSONArray) {
                JSONArray names = (JSONArray) namesObj;
                for (Object name : names) {
                    o = name;
                    if (o instanceof JSONObject) {
                        JSONObject json = (JSONObject) o;

                        Object nameObj = json.get("name");
                        Object dateObj = json.get("last-seen");
                        if (nameObj instanceof String && dateObj instanceof String) {
                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                            format.setTimeZone(TimeZone.getTimeZone("UTC"));
                            try {
                                Date date = format.parse((String) dateObj);
                                map.put((String) nameObj, date);
                            } catch (ParseException e) {
                                System.out.println("Could not parse " + dateObj + ": " + e.getMessage());
                            }
                        }
                    }
                }
            }
        }
        return map;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new HashMap<>();
}

From source file:model.Post_store.java

public static String getposttitle(int id) {

    JSONParser parser = new JSONParser();
    String title = "";

    try {/*from ww w  .  j  a  v a  2  s.c  o m*/

        Object obj = parser.parse(new FileReader(root + "posts/" + id + ".json"));

        JSONObject jsonObject = (JSONObject) obj;

        title = (String) jsonObject.get("title");
        //System.out.println(title);

    } catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        System.out.println(e);
    } catch (ParseException e) {
        System.out.println(e);
    }

    return title;

}

From source file:model.Post_store.java

public static String getpostcontent(int id) {

    JSONParser parser = new JSONParser();
    String content = "";

    try {/*ww  w . j a v a  2 s  . co  m*/

        Object obj = parser.parse(new FileReader(root + "posts/" + id + ".json"));

        JSONObject jsonObject = (JSONObject) obj;

        content = (String) jsonObject.get("content");
        //System.out.println(content);

    } catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        System.out.println(e);
    } catch (ParseException e) {
        System.out.println(e);
    }

    return content;

}

From source file:net.paissad.minus.api.TimelineUtils.java

/**
 * @param user/*from w w  w . ja v  a 2s .  co m*/
 * @param gallery - The readerId of the gallery to use.
 * @param key - The key for which we want its related value.
 * @param listener
 * @return The value for the specified key.
 * @throws MinusException If the key is unknown, or if the specified gallery
 *             is not found, or if unable to connect correctly to Minus API.
 * @see MinusConstants
 */
static Object getValueFromGallery(final MinusUser user, final MinusGallery gallery, final String key)
        throws MinusException {

    try {

        MinusHttpResponse minusResp = HttpClientUtils.doGet(TIMELINE_HISTORY_URL, null, user.getSessionId(),
                null, STRING);
        String httpResponse = (String) minusResp.getHttpResponse();
        MinusUtils.validateHttpResponse(httpResponse);

        Map<String, Object> jsonResult = JSONUtils.getMapFromJSONString(httpResponse);
        MinusUtils.validateJsonResult(jsonResult);

        @SuppressWarnings("unchecked")
        List<JSONObject> galleriesAsObjects = (List<JSONObject>) jsonResult.get(RESPONSE_KEY_GALLERIES);
        for (JSONObject obj : galleriesAsObjects) {

            String currentReaderId = (String) obj.get(RESPONSE_KEY_READER_ID);
            if (currentReaderId.equals(gallery.getReaderId())) {
                // We got the correct gallery !
                if (!obj.keySet().contains(key)) {
                    throw new MinusException("The key (" + key + ") is unknown for the entity -> " + obj);
                }
                return obj.get(key);
            }
        }
        throw new MinusException("Unable to find the gallery " + gallery);

    } catch (Exception e) {
        throw new MinusException(e.getMessage(), e);
    }
}