List of usage examples for org.json.simple.parser JSONParser JSONParser
JSONParser
From source file:DOMIC.EncodeJson.java
public EncodeJson(String JsonText) { JSONParser jsonParser = new JSONParser(); try {/* www. j av a 2 s . c o m*/ JSONObject jsonObject = (JSONObject) jsonParser.parse(JsonText); JSONArray test = (JSONArray) jsonObject.get("test"); for (int i = 0; i < test.size(); i++) { JSONArray questionBlock; this.questionBlocks.add(new QuestionBlock()); questionBlock = (JSONArray) test.get(i); for (int j = 0; j < questionBlock.size(); j++) { HashMap questions = (HashMap) questionBlock.get(j); if (questions.get("-oa") != null) { this.questionBlocks.get(i).questions.add(new Question((String) questions.get("-qu"))); JSONArray answerList = null; answerList = (JSONArray) questions.get("-oa"); this.questionBlocks.get(i).questions.get(j).addAnswer(true, (String) answerList.get(0)); } if (questions.get("-sa") != null) { this.questionBlocks.get(i).questions.add(new Question((String) questions.get("-qu"))); JSONArray answerList = null; answerList = (JSONArray) questions.get("-sa"); for (int k = 0; k < answerList.size(); k++) { this.questionBlocks.get(i).questions.get(j).addAnswer(true, (String) answerList.get(k)); } } if (questions.get("-ma") != null) { this.questionBlocks.get(i).questions.add(new Question((String) questions.get("-qu"))); JSONArray answerList = null; answerList = (JSONArray) questions.get("-ma"); JSONArray trueAnswer = (JSONArray) answerList.get(0); JSONArray falseAnswer = (JSONArray) answerList.get(1); for (int k = 0; k < trueAnswer.size(); k++) { this.questionBlocks.get(i).questions.get(j).addAnswer(true, (String) trueAnswer.get(k)); } for (int k = 0; k < falseAnswer.size(); k++) { this.questionBlocks.get(i).questions.get(j).addAnswer(false, (String) falseAnswer.get(k)); } } } } System.out.println(""); } catch (ParseException e) { e.printStackTrace(); } }
From source file:com.mtag.traffic.util.JsonTools.java
public static JSONObject requestService(String service) throws JsonServiceException { InputStream is = null;/* w ww. j ava 2s .c o m*/ try { URL url = new URL(service); JSONParser parser = new JSONParser(); is = url.openStream(); JSONObject result = (JSONObject) parser.parse(new BufferedReader(new InputStreamReader(is))); is.close(); return result; } catch (MalformedURLException ex) { throw new JsonServiceException("Bad service address", ex); } catch (IOException ex) { throw new JsonServiceException("Service '" + service + "' not available.", ex); } catch (ParseException ex) { throw new JsonServiceException("Error parsind service '" + service + "'.", ex); } finally { if (is != null) { try { is.close(); } catch (IOException ex) { throw new JsonServiceException("unable to close input stream", ex); } } } }
From source file:edu.isi.techknacq.topics.readinglist.ReadDocumentkey.java
public void Readfile() { try {/*from w w w .j av a 2 s .co m*/ JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(filename)); for (Object key : jsonObject.keySet()) { JSONObject documentInfo = (JSONObject) jsonObject.get(key); String author = (String) documentInfo.get("author"); String title = (String) documentInfo.get("title"); docMap.put((String) key, "author: " + author + ", title: " + title); } } catch (FileNotFoundException ex) { Logger.getLogger(ReadDocumentkey.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ReadDocumentkey.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(ReadDocumentkey.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.au.splashinc.JControl.Load.JsonLoader.java
@Override public void LoadConfig() { JSONParser parser = new JSONParser(); try {/* ww w.ja v a2 s .c o m*/ Object obj = parser.parse(new FileReader(location)); JSONObject jsonObject = (JSONObject) obj; //Do stuff probably in another class } catch (IOException | ParseException ex) { Logger.getLogger(JsonLoader.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:mikolaj.torrent.actions.Result.java
public JSONObject fromJson(String data) { try {/* ww w. j a va 2s . c o m*/ JSONParser parser = new JSONParser(); Object obj = parser.parse(data); return (JSONObject) obj; } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.serena.rlc.provider.servicenow.domain.Incident.java
public static Incident parseSingle(String options) { JSONParser parser = new JSONParser(); try {//from w w w . ja va 2s . c o m Object parsedObject = parser.parse(options); Incident incident = parseSingle((JSONObject) getJSONValue((JSONObject) parsedObject, "result")); return incident; } catch (ParseException e) { logger.error("Error while parsing input JSON - " + options, e); } return null; }
From source file:currencyconverter.CurrencyConfiguration.java
/** * @param args the command line arguments *///w ww. ja v a 2 s .co m public CurrencyConfiguration(String file) { JSONParser parser = new JSONParser(); units = new ArrayList<>(); names = new ArrayList<>(); try { JSONObject jsonObject; InputStream is = this.getClass().getResourceAsStream("/" + file); jsonObject = (JSONObject) parser.parse(getStringFromInputStream(is)); //jsonObject = (JSONObject) parser.parse(is); //Object obj = parser.parse(new FileReader("cc_test.json")); //JSONObject jsonObject = (JSONObject) obj; JSONArray junits = (JSONArray) jsonObject.get("units"); Iterator<JSONObject> iterator = junits.iterator(); while (iterator.hasNext()) { JSONObject all = iterator.next(); String country = (String) all.get("CountryCurrency"); String unit = (String) all.get("Units"); units.add(unit); names.add(country); } } catch (Exception e) { e.printStackTrace(); } }
From source file:br.net.fabiozumbi12.RedProtect.hooks.MojangUUIDs.java
public static String getName(String UUID) { try {//w w w . j ava 2s . c o m URL url = new URL("https://api.mojang.com/user/profiles/" + UUID.replaceAll("-", "") + "/names"); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String line = in.readLine(); if (line == null) { return null; } JSONArray array = (JSONArray) new JSONParser().parse(line); HashMap<Long, String> names = new HashMap<Long, String>(); String name = ""; for (Object profile : array) { JSONObject jsonProfile = (JSONObject) profile; if (jsonProfile.containsKey("changedToAt")) { names.put((long) jsonProfile.get("changedToAt"), (String) jsonProfile.get("name")); continue; } name = (String) jsonProfile.get("name"); } if (!names.isEmpty()) { Long key = Collections.max(names.keySet()); return names.get(key); } else { return name; } } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:com.serena.rlc.provider.servicenow.domain.ChangeTask.java
public static ChangeTask parseSingle(String options) { JSONParser parser = new JSONParser(); try {//ww w . ja v a 2s . c om Object parsedObject = parser.parse(options); ChangeTask changeTask = parseSingle((JSONObject) getJSONValue((JSONObject) parsedObject, "result")); return changeTask; } catch (ParseException e) { logger.error("Error while parsing input JSON - " + options, e); } return null; }
From source file:iracing.webapi.HostedSessionResultSummaryParser.java
static long parse(String json, ItemHandler handler) { JSONParser parser = new JSONParser(); // System.err.println(json); long output = 0; if (!"{}".equals(json)) { try {/*from ww w. java 2s.com*/ JSONObject root = (JSONObject) parser.parse(json); output = getLong(root, "rowcount"); JSONArray rootArray = (JSONArray) root.get("rows"); for (int i = 0; i < rootArray.size(); i++) { JSONObject r = (JSONObject) rootArray.get(i); HostedSessionResultSummary summary = new HostedSessionResultSummary(); summary.setWasPrivate((getInt(r, "private")) == 1); summary.setDriverSearchedBestLapTime(getLong(r, "bestlaptime")); summary.setDriverSearchedStartingPosition(getInt(r, "startingposition")); summary.setDriverSearchedClassStartingPosition(getInt(r, "classstartingposition")); summary.setDriverSearchedFinishingPosition(getInt(r, "finishingposition")); summary.setDriverSearchedClassFinishingPosition(getInt(r, "classfinishingposition")); summary.setDriverSearchedCarClassId(getInt(r, "carclassid")); summary.setDriverSearchedIncidents(getInt(r, "incidents")); summary.setPracticeLength(getInt(r, "practicelength")); summary.setQualifyingLaps(getInt(r, "qualifylaps")); summary.setQualifyingLength(getInt(r, "qualifylength")); summary.setRaceLaps(getInt(r, "racelaps")); summary.setRaceLength(getInt(r, "racelength")); summary.setStartTime(new Date(getLong(r, "start_time"))); summary.setMinimumLicenseLevel(getInt(r, "minliclevel")); summary.setMaximumLicenseLevel(getInt(r, "maxliclevel")); summary.setMinimumIrating(getInt(r, "minir")); summary.setMaximumIrating(getInt(r, "maxir")); summary.setPrivateSessionId(getLong(r, "privatesessionid")); IracingCustomer host = new IracingCustomer(); host.setId(getLong(r, "host_custid")); host.setName(getString(r, "host_displayname", true)); summary.setHost(host); summary.setHostLicenseLevel(getInt(r, "host_licenselevel")); summary.setSessionName(getString(r, "sessionname", true)); summary.setTrackId(getInt(r, "trackid")); summary.setTrackName(getString(r, "track_name", true)); IracingCustomer winner = new IracingCustomer(); winner.setId(getLong(r, "winner_custid")); winner.setName(getString(r, "winner_displayname", true)); summary.setWinner(winner); summary.setWinnerLicenseLevel(getInt(r, "winner_licenselevel")); summary.setSessionId(getLong(r, "sessionid")); summary.setSubSessionId(getLong(r, "subsessionid")); summary.setSubSessionFinishedAt(new Date(getLong(r, "subsessionfinishedat"))); summary.setRaceFinishedAt(new Date(getLong(r, "racefinishedat"))); summary.setLoneQualifying((getInt(r, "lonequalify")) == 1); summary.setHardcoreLevelId(getInt(r, "hardcorelevel")); summary.setNightMode((getInt(r, "nightmode")) == 1); summary.setRestarts(getInt(r, "restarts")); summary.setFullCourseCautions((getInt(r, "fullcoursecautions")) == 1); summary.setRollingStarts((getInt(r, "rollingstarts")) == 1); summary.setMultiClass((getInt(r, "multiclass")) == 1); summary.setFixedSetup((getInt(r, "fixed_setup")) == 1); summary.setNumberOfFastTows(getInt(r, "numfasttows")); String s = getString(r, "carids", true); String[] sa = s.split(","); List<Integer> carIds = new ArrayList<Integer>(); for (String carId : sa) { carIds.add(Integer.parseInt(carId)); } summary.setCars(carIds); s = getString(r, "max_pct_fuel_fills", true); if (!"".equals(s)) { sa = s.split(","); List<Integer> maxFuelFills = new ArrayList<Integer>(); for (String mff : sa) { maxFuelFills.add(Integer.parseInt(mff)); } summary.setMaximumPercentageFuelFills(maxFuelFills); } summary.setMaximumDrivers(getInt(r, "maxdrivers")); summary.setCreated(new Date(getLong(r, "created"))); summary.setSessionFastestLap(getLong(r, "sessionfastlap")); summary.setCategoryId(getInt(r, "catid")); handler.onHostedSessionResultSummaryParsed(summary); } } catch (ParseException ex) { Logger.getLogger(HostedSessionResultSummaryParser.class.getName()).log(Level.SEVERE, null, ex); } } return output; }