List of usage examples for org.json.simple.parser JSONParser parse
public Object parse(Reader in) throws IOException, ParseException
From source file:com.apigee.edge.config.mavenplugin.DeveloperMojo.java
public static List getDeveloper(ServerProfile profile) throws IOException { HttpResponse response = RestUtil.getOrgConfig(profile, "developers"); if (response == null) return new ArrayList(); JSONArray developers = null;/*w w w. ja va2 s . co m*/ try { logger.debug("output " + response.getContentType()); // response can be read only once String payload = response.parseAsString(); logger.debug(payload); /* Parsers fail to parse a string array. * converting it to an JSON object as a workaround */ String obj = "{ \"developers\": " + payload + "}"; JSONParser parser = new JSONParser(); JSONObject obj1 = (JSONObject) parser.parse(obj); developers = (JSONArray) obj1.get("developers"); } catch (ParseException pe) { logger.error("Get Developer parse error " + pe.getMessage()); throw new IOException(pe.getMessage()); } catch (HttpResponseException e) { logger.error("Get Developer error " + e.getMessage()); throw new IOException(e.getMessage()); } return developers; }
From source file:com.apigee.edge.config.mavenplugin.TargetServerMojo.java
public static List getTarget(ServerProfile profile) throws IOException { HttpResponse response = RestUtil.getEnvConfig(profile, "targetservers"); if (response == null) return new ArrayList(); JSONArray targets = null;/* ww w . j a v a 2 s. c o m*/ try { logger.debug("output " + response.getContentType()); // response can be read only once String payload = response.parseAsString(); logger.debug(payload); /* Parsers fail to parse a string array. * converting it to an JSON object as a workaround */ String obj = "{ \"targets\": " + payload + "}"; JSONParser parser = new JSONParser(); JSONObject obj1 = (JSONObject) parser.parse(obj); targets = (JSONArray) obj1.get("targets"); } catch (ParseException pe) { logger.error("Get Target Server parse error " + pe.getMessage()); throw new IOException(pe.getMessage()); } catch (HttpResponseException e) { logger.error("Get Target Server error " + e.getMessage()); throw new IOException(e.getMessage()); } return targets; }
From source file:model.Post_store.java
public static List<String> getpostcomments(int id) { JSONParser parser = new JSONParser(); List<String> comments = new ArrayList<>(); try {//from w w w. j a v a 2 s . c om Object obj = parser.parse(new FileReader(root + "posts/" + id + ".json")); JSONObject jsonObject = (JSONObject) obj; JSONArray Jcomments = (JSONArray) jsonObject.get("comments"); if (Jcomments != null) { Iterator<String> iterator = Jcomments.iterator(); while (iterator.hasNext()) { comments.add(iterator.next()); } } } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } catch (ParseException e) { System.out.println(e); } return comments; }
From source file:com.apigee.edge.config.mavenplugin.CacheMojo.java
public static List getCache(ServerProfile profile) throws IOException { HttpResponse response = RestUtil.getEnvConfig(profile, "caches"); if (response == null) return new ArrayList(); JSONArray caches = null;// w w w .ja v a 2s . co m try { logger.debug("output " + response.getContentType()); // response can be read only once String payload = response.parseAsString(); logger.debug(payload); /* Parsers fail to parse a string array. * converting it to an JSON object as a workaround */ String obj = "{ \"caches\": " + payload + "}"; JSONParser parser = new JSONParser(); JSONObject obj1 = (JSONObject) parser.parse(obj); caches = (JSONArray) obj1.get("caches"); } catch (ParseException pe) { logger.error("Get Cache parse error " + pe.getMessage()); throw new IOException(pe.getMessage()); } catch (HttpResponseException e) { logger.error("Get Cache error " + e.getMessage()); throw new IOException(e.getMessage()); } return caches; }
From source file:com.mycompany.craftdemo.utility.java
public static JSONObject getAPIData(String apiURL) { try {/*from www .j a v a 2s . co m*/ URL url = new URL(apiURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); StringBuilder sb = new StringBuilder(); String output; while ((output = br.readLine()) != null) sb.append(output); String res = sb.toString(); JSONParser parser = new JSONParser(); JSONObject json = null; try { json = (JSONObject) parser.parse(res); } catch (ParseException ex) { ex.printStackTrace(); } //resturn api data in json format return json; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:model.Post_store.java
public static List<String> getpostUAcomments(int id) { JSONParser parser = new JSONParser(); List<String> UA_comments = new ArrayList<>(); try {// w w w .j ava2 s.com Object obj = parser.parse(new FileReader(root + "posts/" + id + ".json")); JSONObject jsonObject = (JSONObject) obj; JSONArray JUAcomments = (JSONArray) jsonObject.get("UA_comments"); if (JUAcomments != null) { Iterator<String> iterator = JUAcomments.iterator(); while (iterator.hasNext()) { UA_comments.add(iterator.next()); } } } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } catch (ParseException e) { System.out.println(e); } return UA_comments; }
From source file:iracing.webapi.SpectatorSessionParser.java
static List<SpectatorSession> parse(String json) { JSONParser parser = new JSONParser(); List<SpectatorSession> output = null; try {/*from ww w. j a v a 2 s . c o m*/ JSONArray rootArray = (JSONArray) parser.parse(json); output = new ArrayList<SpectatorSession>(); for (int i = 0; i < rootArray.size(); i++) { JSONObject r = (JSONObject) rootArray.get(i); SpectatorSession o = new SpectatorSession(); o.setSubSessionStatus(getString(r, "subses_status")); o.setSessionStatus(getString(r, "ses_status")); JSONObject b = (JSONObject) r.get("broadcast"); SpectatorSession.BroadcastInfo bi = new SpectatorSession.BroadcastInfo(); bi.setCanBroadcast((Boolean) b.get("canBroadcast")); bi.setSubSessionId(getLong(b, "subSessionId")); bi.setBroadcaster((Boolean) b.get("isBroadcaster")); bi.setMaximumUsers(getInt(b, "maxUsers")); bi.setAvailableSpectatorSlots(getInt(b, "availSpectatorSlots")); bi.setNumberOfDrivers(getInt(b, "numDrivers")); bi.setNumberOfDriverSlots(getInt(b, "numDriverSlots")); bi.setNumberOfSpectators(getInt(b, "numSpectators")); bi.setNumberOfBroadcasters(getInt(b, "numBroadcasters")); bi.setNumberOfSpectatorSlots(getInt(b, "numSpectatorSlots")); bi.setAvailableReservedBroadcasterSlots(getInt(b, "availReservedBCasterSlots")); bi.setCanWatch((Boolean) b.get("canWatch")); o.setBroadcastInfo(bi); o.setSubSessionId(getLong(r, "subsessionid")); o.setSessionId(getLong(r, "sessionid")); o.setSeriesId(getInt(r, "seriesid")); o.setTrackId(getInt(r, "trackid")); JSONObject h = (JSONObject) r.get("hosted"); if (h != null) { SpectatorSession.HostedInfo hi = new SpectatorSession.HostedInfo(); hi.setHeatFilterAmount(getInt(h, "heatFilterAmount")); hi.setPracticeLength(getInt(h, "practiceLength")); JSONArray a = (JSONArray) h.get("admins"); List<SpectatorSession.DriverInfo> driverList = new ArrayList<SpectatorSession.DriverInfo>(); for (int j = 0; j < a.size(); j++) { JSONObject ar = (JSONObject) a.get(j); if (ar != null) { SpectatorSession.DriverInfo di = new SpectatorSession.DriverInfo(); di.setCustomerId(getLong(ar, "custId")); di.setName(getString(ar, "displayName", true)); driverList.add(di); } } hi.setAdministrators(driverList); hi.setOrderId(getLong(h, "orderId")); hi.setMaximumIrating(getInt(h, "maxIR")); hi.setMinimumLicenseLevel(getInt(h, "minLicLevel")); hi.setRootPrivateSessionId(getLong(h, "rootPrivateSessionId")); hi.setDescription(getString(h, "desc", true)); hi.setFastTows(getInt(h, "fastTows")); hi.setQualifyingLength(getInt(h, "qualLength")); hi.setRaceLength(getInt(h, "raceLength")); hi.setRestrictResults((getInt(h, "restrictResults")) == 1); hi.setHardcoreLevelId(getInt(h, "hardcoreLevel")); hi.setId(getLong(h, "id")); hi.setMaximumLicenseLevel(getInt(h, "maxLicLevel")); hi.setQualifyingLaps(getInt(h, "qualLaps")); hi.setHostName(getString(h, "host", true)); // TODO: handle allowed clubs/leagues/teams hi.setRaceLaps(getInt(h, "raceLaps")); hi.setNumberOfServers(getInt(h, "numServers")); hi.setCustomerId(getLong(h, "custid")); // TODO: handle league info hi.setHeatGridType(getInt(h, "heatGridType")); hi.setExpires(new Date(getLong(h, "expires"))); hi.setHeatAddedDrivers(getInt(h, "heatAddedDrivers")); hi.setNumberOfServersFinished(getInt(h, "numServersFinished")); hi.setMultiClass((getInt(h, "multiClass")) == 1); hi.setRestrictViewing((getInt(h, "restrictViewing")) == 1); hi.setSessionName(getString(h, "sessionName", true)); List<Integer> carList = new ArrayList<Integer>(); JSONArray ca = (JSONArray) h.get("carIds"); for (int j = 0; j < ca.size(); j++) { carList.add(((Long) ca.get(j)).intValue()); } hi.setCars(carList); hi.setReason(getString(h, "reason", true)); hi.setTrackId(getInt(h, "trackId")); hi.setLaunchAt(new Date(getLong(h, "launchAt"))); hi.setMaximumDrivers(getInt(h, "maxDrivers")); hi.setSessionId(getLong(h, "sessionId")); hi.setHeatGridsId(getInt(h, "heatGridsId")); hi.setParentPrivateSessionId(getLong(h, "parentPrivateSessionId")); hi.setNightMode((getInt(h, "nightMode")) == 1); hi.setHeatSessionType(getInt(h, "heatSessionType")); hi.setHeatFilterType(getInt(h, "heatFilterType")); hi.setHeatFinal((getInt(h, "heatFinal")) == 1); hi.setCreated(new Date(getLong(h, "created"))); hi.setCautions((getInt(h, "cautions")) == 1); hi.setFixedSetup((getInt(h, "fixedSetup")) == 1); hi.setRestartType(getInt(h, "restartType")); hi.setTimeLimit(getInt(h, "timeLimit")); hi.setPasswordProtected((Boolean) h.get("pwdProtected")); hi.setFarmId(getInt(h, "farmId")); hi.setStatus(getInt(h, "status")); hi.setLoneQualifying((getInt(h, "loneQualify")) == 1); hi.setRollingStart((getInt(h, "rollingStarts")) == 1); hi.setNumberOfServersStarted(getInt(h, "numServersStarted")); JSONArray ff = (JSONArray) h.get("maxPercentFuelFills"); List<Integer> fuelList = new ArrayList<Integer>(); for (int j = 0; j < ff.size(); j++) { fuelList.add(((Long) ff.get(j)).intValue()); } // TODO: finish handling % fuel fills hi.setMinimumIrating(getInt(h, "minIR")); o.setHostedInfo(hi); } o.setStartTime(new Date(getLong(r, "start_time"))); o.setEventTypeId(getInt(r, "evttype")); o.setPrivateSessionId(getLong(r, "privatesessionid")); o.setSeasonId(getInt(r, "seasonid")); output.add(o); } } catch (ParseException ex) { Logger.getLogger(SpectatorSessionParser.class.getName()).log(Level.SEVERE, null, ex); } return output; }
From source file:it.polimi.logging.LogMessageUtils.java
public static JSONObject getStatus(String message) { JSONObject jsonMsg;// ww w . j ava 2s. c om JSONParser parser = new JSONParser(); try { jsonMsg = (JSONObject) parser.parse(message); JSONObject status = (JSONObject) jsonMsg.get(JsonStrings.STATUS); return status; } catch (ParseException e) { e.printStackTrace(); } return null; }
From source file:com.apigee.edge.config.mavenplugin.AppMojo.java
public static List getApp(ServerProfile profile, String developerId) throws IOException { HttpResponse response = RestUtil.getOrgConfig(profile, "developers/" + developerId + "/apps"); if (response == null) return new ArrayList(); JSONArray apps = null;/*from ww w . j av a 2s . c om*/ try { logger.debug("output " + response.getContentType()); // response can be read only once String payload = response.parseAsString(); logger.debug(payload); /* Parsers fail to parse a string array. * converting it to an JSON object as a workaround */ String obj = "{ \"apps\": " + payload + "}"; JSONParser parser = new JSONParser(); JSONObject obj1 = (JSONObject) parser.parse(obj); apps = (JSONArray) obj1.get("apps"); } catch (ParseException pe) { logger.error("Get App parse error " + pe.getMessage()); throw new IOException(pe.getMessage()); } catch (HttpResponseException e) { logger.error("Get Apps error " + e.getMessage()); throw new IOException(e.getMessage()); } return apps; }
From source file:com.jilk.ros.rosbridge.implementation.JSON.java
private static JSONObject convertStringToJSONObject(String json) { JSONObject result = null;/*from ww w . j ava 2s .com*/ StringReader r = new StringReader(json); JSONParser jp = new JSONParser(); try { result = (JSONObject) jp.parse(r); } catch (Throwable t) { System.out.println(t.getMessage()); } r.close(); return result; }