List of usage examples for org.json.simple JSONObject get
V get(Object key);
From source file:ch.newscron.encryption.Encryption.java
/** * Given a String, it is decoded and the result is returned as a String as well. * @param encodedUrl is a String that have the full data encrypted * @return decoded String/* www. j a va2s . c o m*/ */ public static String decode(String encodedUrl) { try { //Decode URL final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(initializationVector.getBytes("UTF-8"))); String result = new String(cipher.doFinal(Base64.decodeBase64(encodedUrl))); //Extract and remove hash from JSONObject JSONParser parser = new JSONParser(); JSONObject receivedData = (JSONObject) parser.parse(result); String receivedHash = (String) receivedData.get("hash"); receivedData.remove("hash"); //Compare received hash with newly computed hash if (checkDataValidity(receivedData)) { byte[] hashOfData = createMD5Hash(receivedData); if (receivedHash.equals(new String(hashOfData, "UTF-8"))) { //Valid data return receivedData.toString(); } } } catch (Exception e) { } //Invalid data (including encryption algorithm exceptions return null; }
From source file:formatter.handler.FormatterHandler.java
/** * Get the document body of the given urn or null * @param db the database where it is/* ww w .j a va 2 s .c o m*/ * @param docID the docID of the resource * @return the document body or null if not present */ private static String getDocumentBody(String db, String docID) throws FormatterException { try { String jStr = Connector.getConnection().getFromDb(db, docID); if (jStr != null) { JSONObject jDoc = (JSONObject) JSONValue.parse(jStr); if (jDoc != null) { Object body = jDoc.get(JSONKeys.BODY); if (body != null) return body.toString(); } } throw new Exception("document " + db + "/" + docID + " not found"); } catch (Exception e) { throw new FormatterException(e); } }
From source file:io.github.seanboyy.lotReset.json.ReadJSON.java
/**Read JSON file by using configuration file's JSON value, * which points to the file/*from w w w . jav a 2 s . co m*/ * @param configLocation String which specifies the location of the config.properties file * @return <code>ArrayList<ArrayList<Lot>> lots</code> if no errors are encountered * @since 1.0 - Implemented config.properties in 2.0 */ public static ArrayList<ArrayList<Lot>> read(String configLocation) { JSONParser parser = new JSONParser(); URL url; File f; //lots is multidimensional in this format: {{fromLot,toLot},{fromLot,toLot},{fromLot,toLot},etc...} ArrayList<ArrayList<Lot>> lots = new ArrayList<ArrayList<Lot>>(); Config config = ReadConfig.read(configLocation); final String[] alphabet = config.getAlpha(); final String[] types = config.getType(); final String[] worlds = config.getWorld(); try { url = new URL(config.getJSON()); InputStream in = url.openStream(); f = File.createTempFile("temp", "json"); FileWriter file = new FileWriter(f); Scanner input = new Scanner(in); while (input.hasNextLine()) { file.write(input.nextLine()); } input.close(); file.close(); Object obj = parser.parse(new FileReader(f)); JSONObject jsonObj = (JSONObject) obj; JSONObject regions = (JSONObject) jsonObj.get("Regions"); for (String a : worlds) { for (String b : types) { for (String c : alphabet) { for (int d = 1; d <= alphabet.length; ++d) { String lotId = a + "-" + b + "-" + c + d; String lotIdA = a + "_" + b + "-" + c + d; String lotIdB = a + "-" + b + "_" + c + d; String lotIdC = a + "_" + b + "_" + c + d; JSONObject lot = (JSONObject) regions.get(lotId); JSONObject lotA = (JSONObject) regions.get(lotIdA); JSONObject lotB = (JSONObject) regions.get(lotIdB); JSONObject lotC = (JSONObject) regions.get(lotIdC); ArrayList<Lot> lotInfo = new ArrayList<Lot>(); if (lot != null) { lotInfo.add(new Lot((long) lot.get("source_minX"), (long) lot.get("source_maxX"), (long) lot.get("source_minY"), (long) lot.get("source_maxY"), (long) lot.get("source_minZ"), (long) lot.get("source_maxZ"), (String) lot.get("source_file"), lotId)); lotInfo.add(new Lot((long) lot.get("dest_minX"), (long) lot.get("dest_maxX"), (long) lot.get("dest_minY"), (long) lot.get("dest_maxY"), (long) lot.get("dest_minZ"), (long) lot.get("dest_maxZ"), (String) lot.get("dest_file"), lotId)); lots.add(lotInfo); } if (lotA != null) { lotInfo.add(new Lot((long) lotA.get("source_minX"), (long) lotA.get("source_maxX"), (long) lotA.get("source_minY"), (long) lotA.get("source_maxY"), (long) lotA.get("source_minZ"), (long) lotA.get("source_maxZ"), (String) lotA.get("source_file"), lotIdA)); lotInfo.add(new Lot((long) lotA.get("dest_minX"), (long) lotA.get("dest_maxX"), (long) lotA.get("dest_minY"), (long) lotA.get("dest_maxY"), (long) lotA.get("dest_minZ"), (long) lotA.get("dest_maxZ"), (String) lotA.get("dest_file"), lotIdA)); lots.add(lotInfo); } if (lotB != null) { lotInfo.add(new Lot((long) lotB.get("source_minX"), (long) lotB.get("source_maxX"), (long) lotB.get("source_minY"), (long) lotB.get("source_maxY"), (long) lotB.get("source_minZ"), (long) lotB.get("source_maxZ"), (String) lotB.get("source_file"), lotIdB)); lotInfo.add(new Lot((long) lotB.get("dest_minX"), (long) lotB.get("dest_maxX"), (long) lotB.get("dest_minY"), (long) lotB.get("dest_maxY"), (long) lotB.get("dest_minZ"), (long) lotB.get("dest_maxZ"), (String) lotB.get("dest_file"), lotIdB)); lots.add(lotInfo); } if (lotC != null) { lotInfo.add(new Lot((long) lotC.get("source_minX"), (long) lotC.get("source_maxX"), (long) lotC.get("source_minY"), (long) lotC.get("source_maxY"), (long) lotC.get("source_minZ"), (long) lotC.get("source_maxZ"), (String) lotC.get("source_file"), lotIdC)); lotInfo.add(new Lot((long) lotC.get("dest_minX"), (long) lotC.get("dest_maxX"), (long) lotC.get("dest_minY"), (long) lotC.get("dest_maxY"), (long) lotC.get("dest_minZ"), (long) lotC.get("dest_maxZ"), (String) lotC.get("dest_file"), lotIdC)); lots.add(lotInfo); } } } } } } catch (IOException e) { System.out.println("FILE ERROR FROM READING JSON"); e.printStackTrace(); return null; } catch (ParseException e) { System.out.println("PARSER ERROR FROM READING JSON"); e.printStackTrace(); return null; } return lots; }
From source file: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)); }// w w w . j ava 2s .co 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:at.ait.dme.yuma.suite.apps.core.server.annotation.JSONAnnotationHandler.java
private static User parseUser(JSONObject jsonObject) { User user = new User((String) jsonObject.get(KEY_USER_NAME)); user.setGravatarHash((String) jsonObject.get(KEY_USER_HASH)); user.setUri((String) jsonObject.get(KEY_USER_URI)); return user;/*from ww w. ja va 2 s . c om*/ }
From source file:net.paissad.minus.api.TimelineUtils.java
/** * Read The specified <tt>Map</tt> and retrieve the gallery entities. * // ww w. ja v a 2 s . com * @param user * @param jsonResult - The Map (this is a JSONObject in reality) * @param verifyEqualityOfUsernames - If <tt>true</tt> then verify that the * names of the creator of each retrieved gallery is always equal * to the name of the user (<code>user.getUsername()</code>). * <b>Note</b>: For example, in "/pane/feed.json", we can have * galleries of many different users, but in "/pane/mine.json", * we should only have the user's own gallery. That's why i think * it's a good practice to do checks. * @return The list of galleries, or an empty list if no JSONObject found. */ private static List<MinusGallery> getGalleriesFromMap(final MinusUser user, final Map<String, Object> jsonResult, final boolean verifyEqualityOfUsernames) { List<MinusGallery> galleries = new ArrayList<MinusGallery>(); @SuppressWarnings("unchecked") List<JSONObject> galleriesAsObjects = (List<JSONObject>) jsonResult.get(RESPONSE_KEY_GALLERIES); for (JSONObject obj : galleriesAsObjects) { String readerId = (String) obj.get(RESPONSE_KEY_READER_ID); String editorId = (String) obj.get(RESPONSE_KEY_EDITOR_ID); String creator = (String) obj.get(RESPONSE_KEY_CREATOR); if (creator == null || creator.trim().isEmpty()) { throw new IllegalStateException("A gallery should contains the name of its creator."); } if (verifyEqualityOfUsernames) { if (!creator.equalsIgnoreCase(user.getUsername())) { throw new IllegalStateException( "The name of the gallery's creator should not be different from the user's name"); } } MinusGallery oneGallery = new MinusGallery(user, editorId, readerId); galleries.add(oneGallery); } return galleries; }
From source file:ci6226.buildindex.java
/** * Indexes the given file using the given writer, or if a directory is * given, recurses over files and directories found under the given * directory./*ww w.ja va 2 s .c o m*/ * * NOTE: This method indexes one document per input file. This is slow. For * good throughput, put multiple documents into your input file(s). An * example of this is in the benchmark module, which can create "line doc" * files, one document per line, using the * <a * href="../../../../../contrib-benchmark/org/apache/lucene/benchmark/byTask/tasks/WriteLineDocTask.html" * >WriteLineDocTask</a>. * * @param writer Writer to the index where the given file/dir info will be * stored * @param file The file to index, or the directory to recurse into to find * files to index * @throws IOException If there is a low-level I/O error */ static void indexDocs(IndexWriter writer, String s) throws IOException { Object obj = JSONValue.parse(s); JSONObject person = (JSONObject) obj; String text = (String) person.get("text"); // System.out.println(text); String user_id = (String) person.get("user_id"); // System.out.println(user_id); String business_id = (String) person.get("business_id"); // System.out.println(business_id); String review_id = (String) person.get("review_id"); // System.out.println(review_id); JSONObject votes = (JSONObject) person.get("votes"); long funny = (Long) votes.get("funny"); // System.out.println(funny); long cool = (Long) votes.get("cool"); // System.out.println(cool); long useful = (Long) votes.get("useful"); // System.out.println(useful); // do not try to index files that cannot be read // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a // field that is indexed (i.e. searchable), but don't tokenize // the field into separate words and don't index term frequency // or positional information: Field review_idf = new StringField("review_id", review_id, Field.Store.YES); doc.add(review_idf); Field business_idf = new StringField("business_id", business_id, Field.Store.YES); doc.add(business_idf); Field textf = new TextField("text", text, Field.Store.YES); doc.add(textf); Field user_idf = new StringField("user_id", user_id, Field.Store.YES); doc.add(user_idf); // Add the last modified date of the file a field named "modified". // Use a LongField that is indexed (i.e. efficiently filterable with // NumericRangeFilter). This indexes to milli-second resolution, which // is often too fine. You could instead create a number based on // year/month/day/hour/minutes/seconds, down the resolution you require. // For example the long value 2011021714 would mean // February 17, 2011, 2-3 PM. doc.add(new LongField("cool", cool, Field.Store.YES)); doc.add(new LongField("funny", funny, Field.Store.YES)); doc.add(new LongField("useful", useful, Field.Store.YES)); // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in UTF-8 encoding. // If that's not the case searching for special characters will fail. // if (writer.getConfig().getOpenMode() == OpenMode.CREATE) { // New index, so we just add the document (no old document can be there): // System.out.println("adding " + review_id); writer.addDocument(doc); // } else { // Existing index (an old copy of this document may have been indexed) so // we use updateDocument instead to replace the old one matching the exact // path, if present: // System.out.println("updating " + file); /// writer.updateDocument(new Term("review_id", review_id), doc); // } }
From source file:com.storageroomapp.client.AccountInfo.java
/** * Parses a String of json text and returns an AccountInfo object. * It will correctly parse the AccountInfo object if it is toplevel, * or also if nested in an 'account' key-value pair. * /*from w w w . j a v a2 s .c o m*/ * @param json the String with the json text * @return an AccountInfo object, or null if the parsing failed */ static public AccountInfo parseJson(String json) { JSONObject root = (JSONObject) JSONValue.parse(json); if (root == null) { return null; } JSONObject account = (JSONObject) root.get("account"); if (account == null) { return null; } return parseJsonObject(account); }
From source file:com.storageroomapp.client.util.JsonSimpleUtil.java
/** * Convenience method for pulling a String 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 String value, or null if not found */// w ww .j av a2 s .co m static public String parseJsonStringValue(JSONObject obj, String key) { if ((obj == null) || (key == null)) { return null; } String value = null; Object valueObj = obj.get(key); if (valueObj != null) { value = valueObj.toString(); } return value; }
From source file:mini_mirc_client.Mini_mirc_client.java
public static void showMsg() { try {/* ww w . j a v a 2 s . c o m*/ synchronized (allMsg) { if (!allMsg.isEmpty() && allMsg.size() > 0) { for (int i = 0; i < allMsg.size(); i++) { JSONObject temp = (JSONObject) allMsg.get(i); JSONArray tempArr = (JSONArray) temp.get("msg"); for (int j = 0; j < tempArr.size(); j++) { temp = (JSONObject) tempArr.get(j); SimpleDateFormat formatDate = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); Date sendDat = new Date(); sendDat.setTime((long) temp.get("timestamp")); System.out .println(">> [" + temp.get("channel").toString() + "] [" + temp.get("username") + "] " + temp.get("message") + " || " + formatDate.format(sendDat)); } } } allMsg.clear(); } } catch (Exception E) { E.printStackTrace(); } }