List of usage examples for org.json.simple JSONArray get
public E get(int index)
From source file:mysynopsis.JSONReader.java
public static void readData() throws FileNotFoundException, IOException, ParseException { try {/*from w w w .j a v a 2 s . c o m*/ JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader("data.json")); JSONObject read; read = (JSONObject) obj; name = (String) read.get("Name"); initial = (String) read.get("Initial"); biog = (String) read.get("Bio"); designation = (String) read.get("Designation"); dept = (String) read.get("Department"); university = (String) read.get("University"); universityAddress = (String) read.get("University_Address"); office = (String) read.get("Office"); officehours = (String) read.get("Ohours"); phone = (String) read.get("Phone"); email = (String) read.get("Email"); resumelink = (String) read.get("Resume_Link"); education = (String) read.get("Educational"); professional = (String) read.get("Professional"); awards = (String) read.get("Awards"); imgExtension = (String) read.get("Image_Ext"); imgString = (String) read.get("Image_String"); /*JSONArray education = (JSONArray) read.get("Education"); for(int i=0;i<8;i++) { educ[i] = (String) education.get(i); }*/ JSONArray ftpinfo = (JSONArray) read.get("Server_Information"); server = (String) ftpinfo.get(0); user = (String) ftpinfo.get(1); dir = (String) ftpinfo.get(2); } catch (IOException | ParseException e) { JOptionPane.showMessageDialog(null, "Something went wrong. Please re-run the software."); } }
From source file:iracing.webapi.SeasonQualifyingResultParser.java
public static long parse(String json, ItemHandler handler) { JSONParser parser = new JSONParser(); long output = 0; try {/*from ww w. j av a2s .c o m*/ JSONObject root = (JSONObject) parser.parse(json); //{"m":{"1":"bestqualtime","2":"sublevel","3":"custrow","4":"maxlicenselevel","5":"rank","6":"helmcolor1","7":"displayname","8":"helmcolor2","9":"division","10":"custid","11":"helmcolor3","12":"clubid","13":"helmpattern","14":"pos","15":"rowcount","16":"bestqualtimeformatted","17":"rn"},"d":{"3":"-1","15":6,"r":[{"1":1465493,"2":"+2.36","4":18,"5":1,"6":255,"7":"Mitchell+Abrahall","8":118,"9":1,"10":25765,"11":108,"12":34,"13":62,"14":1,"16":"2%3A26.549","17":1},{"1":1490441,"2":"+3.43","4":15,"5":2,"6":255,"7":"John+Briggs","8":255,"9":5,"10":73770,"11":255,"12":34,"13":12,"14":2,"16":"2%3A29.044","17":2},{"1":1496234,"2":"+4.51","4":16,"5":3,"6":255,"7":"Stephen+Jenkins","8":96,"9":3,"10":68792,"11":112,"12":34,"13":22,"14":3,"16":"2%3A29.623","17":3},{"1":1502289,"2":"+3.45","4":14,"5":4,"6":240,"7":"Dirk+Benecke","8":130,"9":1,"10":84055,"11":255,"12":42,"13":63,"14":4,"16":"2%3A30.228","17":4},{"1":1513869,"2":"+2.73","4":10,"5":5,"6":44,"7":"Wayne+Stroh","8":240,"9":3,"10":88615,"11":185,"12":34,"13":56,"14":5,"16":"2%3A31.386","17":5},{"1":1548511,"2":"+2.61","4":17,"5":6,"6":80,"7":"Kyle+Young","8":249,"9":2,"10":72798,"11":240,"12":14,"13":21,"14":6,"16":"2%3A34.851","17":6}]}} Object o = root.get("d"); if (o instanceof JSONObject) { JSONObject d = (JSONObject) o; output = getLong(d, "15"); JSONArray arrayRoot = (JSONArray) d.get("r"); for (int i = 0; i < arrayRoot.size(); i++) { JSONObject r = (JSONObject) arrayRoot.get(i); SeasonQualifyingResult result = new SeasonQualifyingResult(); result.setBestQualifyingTime(getLong(r, "1")); result.setLicenseSubLevel(getString(r, "2")); result.setMaxLicenseLevel(getInt(r, "4")); result.setRank(getInt(r, "5")); result.setDriverName(getString(r, "7", true)); result.setDivision(getInt(r, "9")); result.setDriverCustomerId(getLong(r, "10")); result.setClubId(getInt(r, "12")); result.setPosition(getLong(r, "14")); result.setBestQualifyingTimeFormatted(getString(r, "16", true)); handler.onSeasonQualifyingResultParsed(result); } } } catch (ParseException ex) { Logger.getLogger(SeasonQualifyingResultParser.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:eu.hansolo.fx.weatherfx.GeoCode.java
/** * Returns name of City for given latitude and longitude * @param LATITUDE//from ww w . j a va 2 s . c o m * @param LONGITUDE * @return name of City for given latitude and longitude */ public static String inverseGeoCode(final double LATITUDE, final double LONGITUDE) { String URL_STRING = new StringBuilder(INVERSE_GEO_CODE_URL).append(LATITUDE).append(",").append(LONGITUDE) .append("&outFormat=json&thumbMaps=false").toString(); StringBuilder response = new StringBuilder(); try { final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection(); final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream())); String inputLine; while ((inputLine = IN.readLine()) != null) { response.append(inputLine).append("\n"); } IN.close(); Object obj = JSONValue.parse(response.toString()); JSONObject jsonObj = (JSONObject) obj; JSONArray results = (JSONArray) jsonObj.get("results"); JSONObject firstResult = (JSONObject) results.get(0); JSONArray locations = (JSONArray) firstResult.get("locations"); JSONObject firstLocation = (JSONObject) locations.get(0); return firstLocation.get("adminArea5").toString(); } catch (IOException ex) { System.out.println(ex); return ""; } }
From source file:iracing.webapi.LicenseParser.java
public static List<License> parse(String json) { JSONParser parser = new JSONParser(); List<License> output = null; try {/* w w w.j a v a 2 s .co m*/ JSONArray root = (JSONArray) parser.parse(json); output = new ArrayList<License>(); for (int i = 0; i < root.size(); i++) { JSONObject o = (JSONObject) root.get(i); License license = new License(); license.setId(getInt(o, "id")); license.setShortName(getString(o, "shortname")); license.setFullName(getString(o, "name", true)); output.add(license); } } catch (ParseException ex) { Logger.getLogger(LicenseParser.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:eu.hansolo.fx.weatherfx.GeoCode.java
/** * Returns a JavaFX Point2D object that contains latitude (y) and longitude(x) of the given * Address./* w w w . j a v a 2 s . co m*/ * Example format for STREET_CITY_COUNTRY: "1060 W. Addison St., Chicago IL, 60613" * @param STREET_CITY_COUNTRY * @return a JavaFX Point2D object that contains latitude(y) and longitude(x) of given address */ public static Point2D geoCode(final String STREET_CITY_COUNTRY) throws UnsupportedEncodingException { String URL_STRING = new StringBuilder(GEO_CODE_URL).append(URLEncoder.encode(STREET_CITY_COUNTRY, "UTF-8")) .append("&thumbMaps=false").toString(); StringBuilder response = new StringBuilder(); try { final HttpsURLConnection CONNECTION = (HttpsURLConnection) new URL(URL_STRING).openConnection(); final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream())); String inputLine; while ((inputLine = IN.readLine()) != null) { response.append(inputLine).append("\n"); } IN.close(); Object obj = JSONValue.parse(response.toString()); JSONObject jsonObj = (JSONObject) obj; JSONArray results = (JSONArray) jsonObj.get("results"); JSONObject firstResult = (JSONObject) results.get(0); JSONArray locations = (JSONArray) firstResult.get("locations"); JSONObject firstLocation = (JSONObject) locations.get(0); JSONObject latLng = (JSONObject) firstLocation.get("latLng"); return new Point2D(Double.parseDouble(latLng.get("lng").toString()), Double.parseDouble(latLng.get("lat").toString())); } catch (IOException ex) { System.out.println(ex); return new Point2D(0, 0); } }
From source file:com.facebook.tsdb.tsdash.server.model.MetricQuery.java
private static String[] decodeArray(JSONArray jsonArray) { String[] array = new String[jsonArray.size()]; for (int i = 0; i < array.length; i++) { array[i] = (String) jsonArray.get(i); }// w w w .j av a2s .c o m return array; }
From source file:iracing.webapi.SessionResultDriverLapsParser.java
static SessionResultDriverLaps parse(String json) { JSONParser parser = new JSONParser(); SessionResultDriverLaps output = null; try {//from ww w .j ava 2s .co m JSONObject root = (JSONObject) parser.parse(json); output = new SessionResultDriverLaps(); JSONObject details = (JSONObject) root.get("details"); output.setLapsForSolo(getInt(details, "nlapsforsolo")); output.setBestNLapsNumber(getInt(details, "bestnlapsnum")); output.setSeasonShortName(getString(details, "seasonShortName", true)); output.setMaximumLicenseLevel(getInt(details, "maxLicenseLevel")); output.setDriverName(getString(details, "displayname", true)); output.setSeasonName(getString(details, "seasonName", true)); output.setSeriesName(getString(details, "seriesName", true)); output.setSeriesShortName(getString(details, "seriesShortName", true)); output.setSessionId(getLong(details, "sessionId")); output.setSubSessionId(getLong(details, "subSessionId")); output.setCarNumber(getString(details, "carNum")); output.setCarId(getInt(details, "carid")); output.setEventTypeId(getInt(details, "eventtype")); output.setEventTypeName(getString(details, "eventTypeName", true)); output.setBestQualifyingLapAt(getInt(details, "bestQualLapAt")); output.setBestQualifyingLapNumber(getInt(details, "bestQualLapNum")); output.setBestLapNumber(getInt(details, "bestLapNum")); output.setBestNLapsTime(getInt(details, "bestNLapsTime")); output.setLapsForQualifying(getInt(details, "nlapsforqual")); output.setBestQualifyingLapTime(getLong(details, "bestQualLapTime")); output.setTrackId(getInt(details, "trackID")); output.setTrackName(getString(details, "trackName", true)); output.setTrackConfigName(getString(details, "trackConfig", true)); output.setEventDate(new Date(getLong(details, "eventDateUTCMilliSecs"))); List<SessionResultDriverLap> lapList = new ArrayList<SessionResultDriverLap>(); JSONArray a = (JSONArray) root.get("laps"); long lastLapTime = 0; for (int i = 0; i < a.size(); i++) { JSONObject r = (JSONObject) a.get(i); SessionResultDriverLap d = new SessionResultDriverLap(); d.setLapNumber(getInt(r, "lapnum")); long lapTime = getLong(r, "time"); d.setLapTime(lapTime - lastLapTime); lastLapTime = lapTime; d.setFlags(getInt(r, "flags")); lapList.add(d); } output.setLaps(lapList); } catch (ParseException ex) { Logger.getLogger(SessionResultDriverLapsParser.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:cc.pinel.mangue.storage.AbstractStorage.java
/** * Finds the manga JSON object from the array * based on the manga id provided./*from w w w . ja va2 s .c om*/ * * @param jsonMangas the array * @param mangaId the manga id * @return the manga, if found */ protected static JSONObject findManga(JSONArray jsonMangas, String mangaId) { for (int i = 0; i < jsonMangas.size(); i++) { JSONObject jsonManga = (JSONObject) jsonMangas.get(i); Object id = jsonManga == null ? null : jsonManga.get("id"); if (id.equals(mangaId)) return jsonManga; } return null; }
From source file:iracing.webapi.TrackParser.java
public static List<Track> parse(String json) { JSONParser parser = new JSONParser(); List<Track> output = null; try {//from w w w.j av a 2 s . co m //[{"config":"Legends+Oval","priority":3,"pkgid":36,"skuname":"Atlanta+Motor+Speedway","retired":0,"price":14.95,"hasNightLighting":false,"name":"Atlanta+Motor+Speedway","lowername":"atlanta+motor+speedway","nominalLapTime":15.5574207,"lowerNameAndConfig":"atlanta+motor+speedway+legends+oval","catid":1,"priceDisplay":"14.95","id":52,"sku":10039},{"config":"Road+Course","priority":2,"pkgid":36,"skuname":"Atlanta+Motor+Speedway","retired":0,"price":14.95,"hasNightLighting":false,"name":"Atlanta+Motor+Speedway","lowername":"atlanta+motor+speedway","nominalLapTime":82.5555344,"lowerNameAndConfig":"atlanta+motor+speedway+road+course","catid":2,"priceDisplay":"14.95","id":51,"sku":10039}] JSONArray root = (JSONArray) parser.parse(json); output = new ArrayList<Track>(); for (int i = 0; i < root.size(); i++) { JSONObject o = (JSONObject) root.get(i); Track track = new Track(); track.setId(getInt(o, "id")); track.setPackageId(getInt(o, "pkgid")); track.setCategoryId(getInt(o, "catid")); track.setPriority(getInt(o, "priority")); track.setSku(getLong(o, "sku")); track.setSkuName(getString(o, "skuname", true)); track.setRetired((getInt(o, "retired")) == 1); track.setName(getString(o, "name", true)); track.setConfigName(getString(o, "config", true)); track.setHasNightLighting((Boolean) o.get("hasNightLighting")); track.setPrice(getDouble(o, "price")); track.setPriceDisplay(getString(o, "priceDisplay")); track.setNominalLapTime(getDouble(o, "nominalLapTime")); output.add(track); } } catch (ParseException ex) { Logger.getLogger(LicenseParser.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:edu.emory.library.viaf.ViafClient.java
/** * Query the VIAF AutoSuggest API to get suggestions matches for * a user-specified search term. Returns an empty list if * no matches were found or if there was an error either making * the request or parsing the response./* w w w. j a va2s . com*/ * * @param term search term * @return list of ViafResource */ public static List<ViafResource> suggest(String term) throws Exception { String uri = String.format("%s/AutoSuggest?query=%s", baseUrl, URLEncoder.encode(term, "UTF-8")); String result = EULHttpUtils.readUrlContents(uri); // todo: handle (at least log) http exceptions here List<ViafResource> resources = new ArrayList<ViafResource>(); try { // parse the JSON and initialize a list of ViafResource objects // viaf autosuggest returns in json format, with a list of results JSONObject json = (JSONObject) new JSONParser().parse(result); JSONArray jsonArray = (JSONArray) json.get("result"); for (int i = 0; i < jsonArray.size(); i++) { JSONObject obj = (JSONObject) jsonArray.get(i); // initialize a ViafResource for each result, using the viaf id and term // results may also include the following identifiers: // lc, dnb, bnf, bne, nkc, nlilat, nla ViafResource vr = new ViafResource((String) obj.get("viafid"), (String) obj.get("term")); resources.add(vr); } } catch (Exception e) { // json parsing error - should just result in any empty resource list // TODO: log the error ? } return resources; }