List of usage examples for org.json.simple.parser JSONParser parse
public Object parse(Reader in) throws IOException, ParseException
From source file:agileinterop.AgileInterop.java
private static JSONObject getRelatedPSRs(String body) throws APIException, ParseException { // Parse body as JSON JSONParser parser = new JSONParser(); JSONArray jsonBody = (JSONArray) parser.parse(body); Map<String, List<String>> data = new HashMap<>(); for (Object psrNumber : jsonBody) { List<String> relatedPSRNumbers = Agile.getRelatedPSRs((String) psrNumber); data.put((String) psrNumber, relatedPSRNumbers); }//from ww w. ja v a 2 s. co m JSONObject obj = new JSONObject(); obj.put("data", data); return obj; }
From source file:gov.nasa.jpl.xdata.nba.impoexpo.parse.ParseUtil.java
public static GameStats parseGameStats(GameStats.Builder gameStats, String string) throws IOException, ParseException { LOG.info("Parsing GameStats: {}", string); FileReader reader = new FileReader(string); // Pare with JSON simple parser JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); GameSummary summary = parseGameSummary(jsonObject); List<LineScore> score = parseLineScore(jsonObject); SeasonSeries series = parseSeasonSeries(jsonObject); LastMeeting meeting = parseLastMeeting(jsonObject); List<PlayerStats> playerStats = parsePlayerStats(jsonObject); List<TeamStats> teamStats = parseTeamStats(jsonObject); List<OtherStats> otherStats = parseOtherStats(jsonObject); List<Officials> officials = parseOfficials(jsonObject); GameInfo info = parseInfo(jsonObject); List<InactivePlayers> inactivePlayers = parseInactivePlayers(jsonObject); AvailableVideo video = parseAvailableVideo(jsonObject); gameStats.setGameSummary(summary).setLineScore(score).setSeasonSeries(series).setLastMeeting(meeting) .setPlayerStats(playerStats).setTeamStats(teamStats).setOtherStats(otherStats) .setOfficials(officials).setGameInfo(info).setInactivePlayers(inactivePlayers) .setAvailableVideo(video).build(); return gameStats.build(); }
From source file:agileinterop.AgileInterop.java
private static JSONObject createRelatedPSRs(String body) throws UnsupportedOperationException, ParseException, APIException { // Parse body as JSON JSONParser parser = new JSONParser(); JSONObject jsonBody = (JSONObject) parser.parse(body); Map<String, List<String>> data = new HashMap<>(); for (Iterator it = jsonBody.entrySet().iterator(); it.hasNext();) { Map.Entry pair = (Map.Entry) it.next(); String psrNumber = (String) pair.getKey(); JSONArray relatedPSRTypes = (JSONArray) pair.getValue(); List<String> relatedPSRNumbers = new ArrayList<>(); for (Object relatedPSRType : relatedPSRTypes) { String relatedPSRNumber = Agile.createRelatedPSR(psrNumber, (String) relatedPSRType); relatedPSRNumbers.add(relatedPSRNumber); }/*from w w w.j a v a2 s. com*/ data.put(psrNumber, relatedPSRNumbers); } JSONObject obj = new JSONObject(); obj.put("data", data); return obj; }
From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java
/** * Example Hierarchy// w ww .j a v a 2s .c o m * orgConfig.apiProducts * * Returns List of * [ {apiProduct1}, {apiProduct2}, {apiProduct3} ] */ public static List getOrgConfig(File configFile, String scope, String resource) throws ParseException, IOException { Logger logger = LoggerFactory.getLogger(ConfigReader.class); JSONParser parser = new JSONParser(); ArrayList out = null; try { BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile)); JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader); if (edgeConf == null) return null; JSONObject scopeConf = (JSONObject) edgeConf.get(scope); if (scopeConf == null) return null; JSONArray configs = (JSONArray) scopeConf.get(resource); if (configs == null) return null; out = new ArrayList(); for (Object config : configs) { out.add(((JSONObject) config).toJSONString()); } } catch (IOException ie) { logger.info(ie.getMessage()); throw ie; } catch (ParseException pe) { logger.info(pe.getMessage()); throw pe; } return out; }
From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java
/** * List of APIs under apiConfig/*from w ww . ja v a2s .c o m*/ */ public static Set<String> getAPIList(File configFile) throws ParseException, IOException { Logger logger = LoggerFactory.getLogger(ConfigReader.class); JSONParser parser = new JSONParser(); ArrayList<String> out = null; try { BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile)); JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader); if (edgeConf == null) return null; JSONObject scopeConf = (JSONObject) edgeConf.get("apiConfig"); if (scopeConf == null) return null; return scopeConf.keySet(); // while( keys.hasNext() ) { // out.add((String)keys.next()); // } } catch (IOException ie) { logger.info(ie.getMessage()); throw ie; } catch (ParseException pe) { logger.info(pe.getMessage()); throw pe; } }
From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java
/** * Example Hierarchy//w w w . j a v a2 s. co m * envConfig.cache.<env>.caches * * Returns List of * [ {cache1}, {cache2}, {cache3} ] */ public static List getEnvConfig(String env, File configFile, String scope, String resource) throws ParseException, IOException { Logger logger = LoggerFactory.getLogger(ConfigReader.class); JSONParser parser = new JSONParser(); ArrayList out = null; try { BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile)); JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader); if (edgeConf == null) return null; JSONObject scopeConf = (JSONObject) edgeConf.get(scope); if (scopeConf == null) return null; JSONObject envConf = (JSONObject) scopeConf.get(env); if (envConf == null) return null; JSONArray configs = (JSONArray) envConf.get(resource); if (configs == null) return null; out = new ArrayList(); for (Object config : configs) { out.add(((JSONObject) config).toJSONString()); } } catch (IOException ie) { logger.info(ie.getMessage()); throw ie; } catch (ParseException pe) { logger.info(pe.getMessage()); throw pe; } return out; }
From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java
/** * API Config// w w w.j a va2s.co m * [ {apiProduct1}, {apiProduct2}, {apiProduct3} ] */ public static List getAPIConfig(File configFile, String api, String resource) throws ParseException, IOException { Logger logger = LoggerFactory.getLogger(ConfigReader.class); JSONParser parser = new JSONParser(); ArrayList out = null; try { BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile)); JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader); if (edgeConf == null) return null; JSONObject scopeConf = (JSONObject) edgeConf.get("apiConfig"); if (scopeConf == null) return null; JSONObject scopedConfigs = (JSONObject) scopeConf.get(api); if (scopedConfigs == null) return null; JSONArray resourceConfigs = (JSONArray) scopedConfigs.get(resource); if (resourceConfigs == null) return null; out = new ArrayList(); for (Object config : resourceConfigs) { out.add(((JSONObject) config).toJSONString()); } } catch (IOException ie) { logger.info(ie.getMessage()); throw ie; } catch (ParseException pe) { logger.info(pe.getMessage()); throw pe; } return out; }
From source file:com.krypc.hl.pr.controller.PatientRecordController.java
public static ResponseWrapper convertQueryResponse(ResponseWrapper wrapper) throws ParseException { if (wrapper.resultObject instanceof ChainCodeResponse) { ChainCodeResponse res = (ChainCodeResponse) wrapper.resultObject; if (res.getStatus() == Status.SUCCESS) { String plain_json = res.getMessage().toStringUtf8(); JSONParser parser = new JSONParser(); JSONObject job = (JSONObject) parser.parse(plain_json); wrapper.resultObject = job.get("resultObject"); wrapper.errorCode = (long) job.get("errorCode"); wrapper.errorDetails = (String) job.get("errorDetails"); wrapper.errorMessage = (String) job.get("errorMessage"); return wrapper; }/* www. j ava 2 s . c om*/ } return null; }
From source file:com.raphfrk.craftproxyclient.net.auth.AuthManager.java
public static JSONObject sendRequest(JSONObject request, String endpoint) { URL url;//from ww w. j a v a 2s . com try { url = new URL(authServer + "/" + endpoint); } catch (MalformedURLException e) { return null; } try { HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setDoOutput(true); con.setInstanceFollowRedirects(false); con.setReadTimeout(5000); con.setConnectTimeout(5000); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json"); con.connect(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(con.getOutputStream(), StandardCharsets.UTF_8)); try { request.writeJSONString(writer); writer.flush(); writer.close(); if (con.getResponseCode() != 200) { return null; } BufferedReader reader = new BufferedReader( new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8)); try { JSONParser parser = new JSONParser(); try { return (JSONObject) parser.parse(reader); } catch (ParseException e) { return null; } } finally { reader.close(); } } finally { writer.close(); con.disconnect(); } } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:IrqaQuery.java
public static void pipeline(String basedir, String indexpath, String set, JSONObject lookup_sent) throws Exception { System.out.println(set + " started..."); String index = basedir + "/index_all" + indexpath + "/"; String stopwords = basedir + "/stopwords.txt"; IrqaQuery lp = new IrqaQuery(); String answer_filename = String.format(basedir + "/stats/data_for_analysis/newTACL/%s_raw_list.json", set); String file = String.format(basedir + "/stats/data_for_analysis/newTACL/WikiQASent-%s.txt", set); // String lookup_8kfn = basedir+"/data/wikilookup_8k.json"; String documents2_fn = basedir + "/data/documents2.json"; JSONParser parser = new JSONParser(); JSONArray answer_list = (JSONArray) parser.parse(new FileReader(answer_filename)); // Object obj2 = parser.parse(new FileReader(lookup_8kfn)); // JSONObject lookup_8k = (JSONObject) obj2; Object obj3 = parser.parse(new FileReader(documents2_fn)); JSONArray documents2 = (JSONArray) obj3; List<String> questions = new ArrayList<>(); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String outfilename = String.format(basedir + "/stats/data_for_analysis/newTACL/newsplit%s_%s.txt", indexpath, set);//w w w.j a va 2 s . co m BufferedWriter outfile = new BufferedWriter(new FileWriter(outfilename)); int numline = 0; ArrayList<ArrayList<String>> sentlistAll = new ArrayList<ArrayList<String>>(); ArrayList<ArrayList<String>> alistAll = new ArrayList<ArrayList<String>>(); try { String r; String cquestion = ""; ArrayList<String> sentlist = new ArrayList<>(); ArrayList<String> alist = new ArrayList<>(); while ((r = br.readLine()) != null) { numline++; String[] line = r.split("\t"); if (cquestion.compareTo(line[0]) != 0) { if (cquestion.compareTo("") != 0) { sentlistAll.add(sentlist); alistAll.add(alist); questions.add(cquestion); } sentlist = new ArrayList<>(); alist = new ArrayList<>(); sentlist.add(line[1]); alist.add(line[2]); cquestion = line[0]; } else { sentlist.add(line[1]); alist.add(line[2]); } } sentlistAll.add(sentlist); alistAll.add(alist); questions.add(cquestion); } finally { br.close(); } System.out.println(questions.size()); for (int i = 0; i < questions.size(); i++) { String query = questions.get(i); List<Document> docs = lp.query(index, stopwords, query, 5, "BM25"); // Object o = (Object) answer_list.get(0); JSONObject rl = (JSONObject) answer_list.get(i); String gold_pid = (String) rl.get("paragraph_id"); // String gold_q =(String) rl.get("question"); for (Document d : docs) { String docid = d.get("docid"); if (gold_pid.compareTo(docid) == 0) { // get sentences from gold (alistAll, sentlistAll) for (int j = 0; j < sentlistAll.get(i).size(); j++) { if (sentlistAll.get(i).get(j).length() < 1 || sentlistAll.get(i).get(j).compareTo(" ") == 0 || sentlistAll.get(i).get(j).compareTo(" ") == 0 || sentlistAll.get(i).get(j).compareTo("''") == 0 || sentlistAll.get(i).get(j).compareTo(" ") == 0) continue; String outstring = String.format("%s\t%s\t%s\n", query, sentlistAll.get(i).get(j), alistAll.get(i).get(j)); outfile.write(outstring); } } else { // get_sentence_from_lookup(); // lookup_sent.get(docid) // JSONArray sents = (JSONArray) lookup_sent.get("Timeline_of_classical_mechanics-Abstract"); JSONArray sents = (JSONArray) lookup_sent.get(docid); if (sents == null) { System.out.println("noway, " + docid + "\n"); } else { for (int kk = 0; kk < sents.size(); kk++) { if (sents.get(kk).toString().length() < 1 || sents.get(kk).toString().compareTo(" ") == 0 || sents.get(kk).toString().compareTo(" ") == 0 || sents.get(kk).toString().compareTo("''") == 0 || sents.get(kk).toString().compareTo(" ") == 0) continue; String outstring = String.format("%s\t%s\t%s\n", query, sents.get(kk).toString(), "0"); outfile.write(outstring); // System.out.printf("%s\t%s\t%s\n", query, sents.get(kk).toString(), "0"); // System.out.println(sents.get(kk)); } } } } } outfile.close(); // System.out.println(raw_list.size()); System.out.println(numline); }