List of usage examples for org.json.simple JSONObject get
V get(Object key);
From source file:iracing.webapi.WorldRecordsParser.java
static WorldRecords parse(String json, boolean includeApiUserRow) { JSONParser parser = new JSONParser(); WorldRecords output = null;/*from w w w . j av a 2 s. c o m*/ try { JSONObject root = (JSONObject) parser.parse(json); output = new WorldRecords(); JSONObject root2 = (JSONObject) root.get("d"); output.setApiUserRow(getLong(root2, "22")); output.setTotalResults(getLong(root2, "32")); JSONArray rootArray = (JSONArray) root2.get("r"); List<WorldRecord> recordList = new ArrayList<WorldRecord>(); for (int i = 0; i < rootArray.size(); i++) { JSONObject r = (JSONObject) rootArray.get(i); long recordNumber = getLong(r, "36"); if (!includeApiUserRow && recordNumber == 0) continue; WorldRecord wr = new WorldRecord(); wr.setClubName(getString(r, "1", true)); wr.setCountryCode(getString(r, "2")); wr.setLicenseSubLevel(getInt(r, "3")); wr.setIrating(getInt(r, "4")); wr.setTimeTrialSubSessionId(getLong(r, "5")); wr.setQualify(getString(r, "6", true)); IracingCustomer driver = new IracingCustomer(); driver.setId(getLong(r, "26")); driver.setName(getString(r, "7", true)); wr.setDriver(driver); long l = getLong(r, "8"); if (l > 0) wr.setPracticeStartTime(new Date(l)); wr.setSeasonQuarter(getInt(r, "9")); wr.setClubId(getInt(r, "10")); wr.setRace(getString(r, "11", true)); wr.setLicenseSubLevelText(getString(r, "13", true)); wr.setSeasonYear(getInt(r, "14")); wr.setTimeTrialRating(getInt(r, "15")); wr.setLicenseGroupId(getInt(r, "16")); wr.setRaceSubSessionId(getLong(r, "17")); l = getLong(r, "18"); if (l > 0) wr.setTimeTrialStartTime(new Date(l)); wr.setQualifyingSubSessionId(getLong(r, "19")); wr.setLicenseLevelId(getInt(r, "20")); wr.setTrackId(getInt(r, "21")); wr.setTimeTrial(getString(r, "23", true)); wr.setCarId(getInt(r, "28")); wr.setCategoryId(getInt(r, "29")); wr.setRegionName(getString(r, "30", true)); wr.setPracticeSubSessionId(getLong(r, "31")); l = getLong(r, "33"); if (l > 0) wr.setRaceStartTime(new Date(l)); wr.setPractice(getString(r, "34", true)); l = getLong(r, "35"); wr.setRecordNumber(recordNumber); if (l > 0) wr.setQualifyingStartTime(new Date(l)); wr.setCategoryName(getString(r, "37")); recordList.add(wr); } output.setRecords(recordList); } catch (ParseException ex) { Logger.getLogger(WorldRecordsParser.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:msuresh.raftdistdb.RaftCluster.java
public static void InitPortNumber() { try {//from w ww. ja va 2 s .co m File f = new File(Constants.STATE_LOCATION + "global.info"); if (!f.exists()) { createDefaultGlobal(); } JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader(Constants.STATE_LOCATION + "global.info")); JSONObject jsonObject = (JSONObject) obj; Long a = (Long) jsonObject.get("currentCount"); portId = a.intValue(); } catch (Exception e) { System.out.println(e.toString()); } }
From source file:com.avatarproject.core.storage.UserCache.java
/** * Adds a player into the custom UserCache. * @param player Player to add to the cache. *///ww w .j a va 2 s . c o m @SuppressWarnings("unchecked") public static void addUser(Player player) { String name = player.getName(); UUID uuid = player.getUniqueId(); JSONArray array = getUserCache(); try { for (int n = 0; n < array.size(); n++) { //Loop through all the objects in the array. JSONObject object = (JSONObject) array.get(n); if (object.get("id").equals(uuid.toString())) { //Check if the player's UUID exists in the cache. if (String.valueOf(object.get("name")).equalsIgnoreCase(name)) { return; } else { object.put("name", name); //Update the user. FileWriter fileOut = new FileWriter(usercache); fileOut.write(array.toJSONString()); //Write the JSON array to the file. fileOut.close(); return; } } } JSONObject newEntry = new JSONObject(); newEntry.put("id", uuid.toString()); newEntry.put("name", name); array.add(newEntry); //Add a new player into the cache. FileWriter fileOut = new FileWriter(usercache); fileOut.write(array.toJSONString()); //Write the JSON array to the file. fileOut.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.appcelerator.titanium.desktop.ui.wizard.Release.java
/** * Given the JSON response from the Cloud service, store the result in the preferences (the releases, the pubDate, * the public link).//from w w w. ja v a 2s.c om * * @param project * @param json */ public static void updateForProject(IProject project, JSONObject json) { final JSONArray releases = (JSONArray) json.get("releases"); //$NON-NLS-1$ final String appPage = (String) json.get(APP_PAGE_PREF_KEY); final String pubDate = (String) json.get(PUBDATE_PREF_KEY); // delete current rows deletePackagesForProject(project); // insert new rows for (Object r : releases) { String label = (String) ((JSONObject) r).get(LABEL_PREF_KEY); String url = (String) ((JSONObject) r).get(URL_PREF_KEY); String platform = (String) ((JSONObject) r).get(PLATFORM_PREF_KEY); String version = (String) ((JSONObject) r).get(VERSION_PREF_KEY); addPackageToDatabase(project, url, label, platform, version, pubDate, appPage); } }
From source file:com.memetix.gun4j.expand.UrlExpander.java
private static Map<String, String> parseResponse(final JSONObject json) { Map<String, String> expandedResults = new HashMap<String, String>(); final JSONObject data = (JSONObject) json.get("data"); if (data.containsKey("expand")) { final JSONArray expand = (JSONArray) data.get("expand"); for (Object result : expand) { final JSONObject expandedResult = (JSONObject) result; expandedResults.put((String) expandedResult.get("shortUrl"), (String) expandedResult.get("fullUrl")); }/* w ww .j a va2s . c o m*/ } return expandedResults; }
From source file:com.rmtheis.yandtran.YandexTranslatorAPI.java
/** * Forms a request, sends it using the GET method and returns the value with the given label from the * resulting JSON response./* w w w . j ava 2 s .c o m*/ */ protected static String retrievePropString(final URL url, final String jsonValProperty) throws Exception { final String response = retrieveResponse(url); JSONObject jsonObj = (JSONObject) JSONValue.parse(response); return jsonObj.get(jsonValProperty).toString(); }
From source file:com.marklogic.tableauextract.ExtractFromJSON.java
/** * Read JSON output from MarkLogic REST extension or *.xqy file and insert * the output into a Tabeleau table//from w ww . j a v a 2 s . c om * * @param table * @throws TableauException */ private static void insertData(Table table) throws TableauException, FileNotFoundException, ParseException, IOException { TableDefinition tableDef = table.getTableDefinition(); Row row = new Row(tableDef); URL url = new URL("http://localhost:8060/json.xqy"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(bufferedReader); JSONArray claims = (JSONArray) jsonObject.get("claims"); @SuppressWarnings("unchecked") Iterator<JSONObject> i = claims.iterator(); while (i.hasNext()) { JSONObject innerObject = i.next(); String idString = (String) innerObject.get("id"); row.setInteger(0, Integer.parseInt(idString.substring(0, 6))); row.setCharString(1, (String) innerObject.get("ssn")); row.setCharString(2, (String) innerObject.get("type")); String payString = (String) innerObject.get("payment_amount"); if (payString == null || payString.isEmpty()) payString = "0.0"; row.setDouble(3, Double.parseDouble(payString)); String dtString = (String) innerObject.get("claim_date"); if (dtString == null || dtString.isEmpty()) dtString = "1999-01-01"; LocalDate claimDate = (LocalDate.parse(dtString)); row.setDate(4, claimDate.getYear(), claimDate.getMonthValue(), claimDate.getDayOfMonth()); table.insert(row); } /* row.setDateTime( 0, 2012, 7, 3, 11, 40, 12, 4550); // Purchased row.setCharString(1, "Beans"); // Product row.setString( 2, "uniBeans"); // uProduct row.setDouble( 3, 1.08); // Price row.setDate( 6, 2029, 1, 1); // Expiration date row.setCharString(7, "Bohnen"); // Produkt for ( int i = 0; i < 10; ++i ) { row.setInteger(4, i * 10); // Quantity row.setBoolean(5, i % 2 == 1); // Taxed } */ }
From source file:com.olhcim.engine.Mesh.java
public static Mesh loadMeshFile(String fileName) { //load and parse file JSONParser parser = new JSONParser(); Object obj = null;/*from w w w. j av a 2 s . com*/ try { obj = parser.parse(new FileReader(AppMain.class.getResource(fileName).getFile().replace("%20", " "))); } catch (IOException ex) { } catch (ParseException ex) { } JSONObject jObj = (JSONObject) obj; //load and convert to double array Object[] verticesObj = ((JSONArray) jObj.get("positions")).toArray(); double[] vertices = new double[verticesObj.length]; for (int i = 0; i < verticesObj.length; i++) { vertices[i] = (double) verticesObj[i]; } //load and convert to long array Object[] indicesObj = ((JSONArray) jObj.get("indices")).toArray(); long[] indices = new long[indicesObj.length]; for (int i = 0; i < indicesObj.length; i++) { indices[i] = (long) indicesObj[i]; } int verticesCount = vertices.length / 3; int facesCount = indices.length / 3; System.out.println("Vertices: " + verticesCount + " Faces: " + facesCount); Mesh mesh = new Mesh("", verticesCount, facesCount); // Filling the Vertices array of our mesh first for (int index = 0; index < verticesCount; index++) { float x = (float) vertices[index * 3]; float y = (float) vertices[index * 3 + 1]; float z = (float) vertices[index * 3 + 2]; mesh.vertices[index] = new Vector4f(x, y, z, 1); } // Then filling the Faces array for (int index = 0; index < facesCount; index++) { float a = (int) indices[index * 3]; float b = (int) indices[index * 3 + 1]; float c = (int) indices[index * 3 + 2]; mesh.faces[index] = new Vector3f(a, b, c); } return mesh; }
From source file:me.ryandowling.allmightybot.utils.TwitchAPI.java
public static void checkToken() { System.out.println("Checking Twitch API token"); TwitchAPIRequest request = new TwitchAPIRequest("/"); try {//from w w w .j a v a2s. com String response = request.get(); JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser.parse(response); JSONObject token = (JSONObject) jsonObject.get("token"); boolean valid = (boolean) token.get("valid"); if (!valid) { System.err.println("API token not valid, exiting!"); System.exit(1); } System.out.println("Token is valid, API connection successful!"); } catch (IOException e) { e.printStackTrace(); System.err.println("Error connecting to Twitch API, exiting!"); System.exit(1); } catch (ParseException e) { e.printStackTrace(); System.err.println("Error occured parsing JSON from Twitch API, exiting!"); System.exit(1); } System.out.println("Finished checking Twitch API token"); }
From source file:com.tom.deleteme.PrintAnnotations.java
/** * Prints all Annotations of a specified Type to a PrintStream. * //from ww w . j a va 2s .c o m * @param aCAS * the CAS containing the FeatureStructures to print * @param aAnnotType * the Type of Annotation to be printed * @param aOut * the PrintStream to which output will be written */ public static void printAnnotatedText(FeatureStructure aFS, CAS aCAS, int aNestingLevel, PrintStream aOut, JSONObject json) { String annotationKey = aFS.getType().getName(); if (json.get(annotationKey) == null) { if (!annotationKey.equals(tcasAnnotationKey)) { if (aFS instanceof AnnotationFS) { AnnotationFS annot = (AnnotationFS) aFS; String coveredText = annot.getCoveredText(); if (annotationKey.equals(WorkExperienceKey)) { coveredText = stringCleanUp(coveredText, "([\\d]{1,})?.?[\\d]{1,}"); } if (annotationKey.equals(PinCodeKey)) { coveredText = stringCleanUp(coveredText, "[\\d]{6}"); } if (coveredText instanceof String) { coveredText = processString(coveredText); } if (annotationKey.equals(WorkExperienceKey)) { json.put(aFS.getType().getName(), Float.parseFloat(coveredText)); } else { json.put(annotationKey, coveredText); } } } } else if ((aFS.getType().getName()).equals(OtherNamedTagsKey)) { if (aFS instanceof AnnotationFS) { AnnotationFS annot = (AnnotationFS) aFS; String coveredText = annot.getCoveredText(); coveredText = processString(coveredText); appendOtherNamedTags(json, coveredText); } } }