List of usage examples for org.json.simple.parser JSONParser parse
public Object parse(Reader in) throws IOException, ParseException
From source file:com.serena.rlc.provider.jira.domain.Issue.java
public static Issue parseSingle(String options) { JSONParser parser = new JSONParser(); try {//from w w w.j a va 2 s. co m Object parsedObject = parser.parse(options); JSONObject jsonObject = (JSONObject) parsedObject; Issue issue = parseSingle(jsonObject); return issue; } catch (ParseException e) { logger.error("Error while parsing input JSON - " + options, e); } return null; }
From source file:com.stratio.es.ESJavaRDDFT.java
/** * Imports dataset/* w w w. j a v a 2 s . c o m*/ * * @throws java.io.IOException */ private static void dataSetImport() throws IOException, ExecutionException, IOException, InterruptedException, ParseException { JSONParser parser = new JSONParser(); URL url = Resources.getResource(DATA_SET_NAME); Object obj = parser.parse(new FileReader(url.getFile())); JSONObject jsonObject = (JSONObject) obj; IndexResponse responseBook = client.prepareIndex(ES_INDEX_BOOK, ES_TYPE_INPUT, "id") .setSource(jsonObject.toJSONString()).execute().actionGet(); String json2 = "{" + "\"message\":\"" + MESSAGE_TEST + "\"" + "}"; IndexResponse response2 = client.prepareIndex(ES_INDEX_MESSAGE, ES_TYPE_MESSAGE).setCreate(true) .setSource(json2).setReplicationType(ReplicationType.ASYNC).execute().actionGet(); String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; IndexResponse response = client.prepareIndex(ES_INDEX, ES_TYPE).setCreate(true).setSource(json).execute() .actionGet(); String index = response.getIndex(); String _type = response.getType(); String _id = response.getId(); try { CountResponse countResponse = client.prepareCount(ES_INDEX).setTypes(ES_TYPE).execute().actionGet(); SearchResponse searchResponse = client.prepareSearch(ES_INDEX_BOOK).setTypes(ES_TYPE_INPUT).execute() .actionGet(); //searchResponse.getHits().hits(); //assertEquals(searchResponse.getCount(), 1); } catch (AssertionError | Exception e) { cleanup(); e.printStackTrace(); } }
From source file:cloudclient.Client.java
public static void batchReceiveResp(BufferedReader in) throws IOException, ParseException { BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt")); JSONParser parser = new JSONParser(); String message;//from w w w . j av a 2s. c o m while ((message = in.readLine()) != null) { //System.out.println(message); JSONArray responseList = (JSONArray) parser.parse(message); for (int i = 0; i < responseList.size(); i++) { JSONObject response = (JSONObject) responseList.get(i); bw.write(response.get("URL").toString()); bw.newLine(); } } bw.close(); }
From source file:mini_mirc_client.Mini_mirc_client.java
public static void updateMsg(miniIRC.Client client) throws TException { String temp = client.regularUpdate(username); temp = temp.replaceAll("\\{\"msg\":\\[\\]\\}", ""); if (!temp.isEmpty() && temp.length() > 6) { try {//from w ww . j av a 2 s. c o m JSONParser J = new JSONParser(); JSONObject jeson = new JSONObject(); jeson = (JSONObject) J.parse(temp); allMsg.add(jeson); } catch (Exception E) { E.printStackTrace(); } } }
From source file:kltn.geocoding.Geocoding.java
private static Geometry getBoundary(String s) throws MalformedURLException, IOException, org.json.simple.parser.ParseException { String link = "https://maps.googleapis.com/maps/api/geocode/json?&key=AIzaSyALCgmmer3Cht-mFQiaJC9yoWdSqvfdAiM"; link = link + "&address=" + URLEncoder.encode(s); URL url = new URL(link); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); InputStream is = httpsCon.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); String jsonString = writer.toString(); JSONParser parse = new JSONParser(); Object obj = parse.parse(jsonString); JSONObject jsonObject = (JSONObject) obj; System.out.println(s);//from w ww. j a v a2 s . c o m System.out.println(jsonObject.toJSONString()); JSONArray resultArr = (JSONArray) jsonObject.get("results"); Object resultObject = parse.parse(resultArr.get(0).toString()); JSONObject resultJsonObject = (JSONObject) resultObject; Object geoObject = parse.parse(resultJsonObject.get("geometry").toString()); JSONObject geoJsonObject = (JSONObject) geoObject; if (!geoJsonObject.containsKey("bounds")) { return null; } Object boundObject = parse.parse(geoJsonObject.get("bounds").toString()); JSONObject boundJsonObject = (JSONObject) boundObject; // System.out.println(boundJsonObject.toJSONString()); Object southwest = parse.parse(boundJsonObject.get("southwest").toString()); JSONObject southwestJson = (JSONObject) southwest; String southwestLat = southwestJson.get("lat").toString(); String southwestLong = southwestJson.get("lng").toString(); Object northeast = parse.parse(boundJsonObject.get("northeast").toString()); JSONObject northeastJson = (JSONObject) northeast; String northeastLat = northeastJson.get("lat").toString(); String northeastLong = northeastJson.get("lng").toString(); String polygon = "POLYGON((" + southwestLong.trim() + " " + northeastLat.trim() + "," + northeastLong.trim() + " " + northeastLat.trim() + "," + northeastLong.trim() + " " + southwestLat.trim() + "," + southwestLong.trim() + " " + southwestLat.trim() + "," + southwestLong.trim() + " " + northeastLat.trim() + "))"; Geometry geo = wktToGeometry(polygon); return geo; }
From source file:com.serena.rlc.provider.jira.domain.Issue.java
public static List<Issue> parse(String options) { List<Issue> list = new ArrayList<Issue>(); JSONParser parser = new JSONParser(); try {// w w w .j a va 2s . c om Object parsedObject = parser.parse(options); JSONArray array = (JSONArray) ((JSONObject) parsedObject).get("issues"); for (Object object : array) { Issue obj = new Issue(); JSONObject jsonObject = (JSONObject) object; JSONObject fieldsObject = (JSONObject) jsonObject.get("fields"); JSONObject typeObject = (JSONObject) fieldsObject.get("issuetype"); JSONObject statusObject = (JSONObject) fieldsObject.get("status"); JSONObject projectObject = (JSONObject) fieldsObject.get("project"); JSONObject creatorObject = (JSONObject) fieldsObject.get("creator"); JSONObject priorityObject = (JSONObject) fieldsObject.get("priority"); obj.setId((String) jsonObject.get("key")); obj.setUrl((String) jsonObject.get("key")); obj.setName((String) fieldsObject.get("summary")); obj.setDescription((String) fieldsObject.get("description")); obj.setDateCreated((String) fieldsObject.get("created")); obj.setLastUpdated((String) fieldsObject.get("updated")); obj.setType((String) typeObject.get("name")); obj.setStatus((String) statusObject.get("name")); obj.setProject((String) projectObject.get("name")); obj.setCreator((String) creatorObject.get("name")); obj.setPriority((String) priorityObject.get("name")); list.add(obj); } } catch (ParseException e) { logger.error("Error while parsing input JSON - " + options, e); } return list; }
From source file:com.mycompany.craftdemo.utility.java
public static JSONObject getConfig(String filePath) { try {/*from w w w . jav a2 s .c om*/ // read the json file FileReader reader = new FileReader(filePath); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); return jsonObject; } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (ParseException ex) { ex.printStackTrace(); } catch (NullPointerException ex) { ex.printStackTrace(); } return null; }
From source file:com.gnapse.metric.CurrencyLoader.java
/** * Loads and parses the JSON code from the given input stream. *//*from ww w . j av a 2 s . c o m*/ private static JSONObject loadJSON(InputStream in) throws IOException, ParseException { Reader reader = new InputStreamReader(in, Charsets.UTF_8); JSONParser parser = new JSONParser(); return (JSONObject) parser.parse(reader); }
From source file:com.wattzap.model.social.SelfLoopsAPI.java
public static int uploadActivity(String email, String passWord, String fileName, String note) throws IOException { JSONObject jsonObj = null;//from www . j a v a 2s. com FileInputStream in = null; GZIPOutputStream out = null; CloseableHttpClient httpClient = HttpClients.createDefault(); try { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("enctype", "multipart/mixed"); in = new FileInputStream(fileName); // Create stream to compress data and write it to the to file. ByteArrayOutputStream obj = new ByteArrayOutputStream(); out = new GZIPOutputStream(obj); // Copy bytes from one stream to the other byte[] buffer = new byte[4096]; int bytes_read; while ((bytes_read = in.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); } out.close(); in.close(); ByteArrayBody bin = new ByteArrayBody(obj.toByteArray(), ContentType.create("application/x-gzip"), fileName); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("email", new StringBody(email, ContentType.TEXT_PLAIN)) .addPart("pw", new StringBody(passWord, ContentType.TEXT_PLAIN)).addPart("tcxfile", bin) .addPart("note", new StringBody(note, ContentType.TEXT_PLAIN)).build(); httpPost.setEntity(reqEntity); CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); int code = response.getStatusLine().getStatusCode(); switch (code) { case 200: HttpEntity respEntity = response.getEntity(); if (respEntity != null) { // EntityUtils to get the response content String content = EntityUtils.toString(respEntity); //System.out.println(content); JSONParser jsonParser = new JSONParser(); jsonObj = (JSONObject) jsonParser.parse(content); } break; case 403: throw new RuntimeException( "Authentification failure " + email + " " + response.getStatusLine()); default: throw new RuntimeException("Error " + code + " " + response.getStatusLine()); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (response != null) { response.close(); } } int activityId = ((Long) jsonObj.get("activity_id")).intValue(); // parse error code int error = ((Long) jsonObj.get("error_code")).intValue(); if (activityId == -1) { String message = (String) jsonObj.get("message"); switch (error) { case 102: throw new RuntimeException("Empty TCX file " + fileName); case 103: throw new RuntimeException("Invalide TCX Format " + fileName); case 104: throw new RuntimeException("TCX Already Present " + fileName); case 105: throw new RuntimeException("Invalid XML " + fileName); case 106: throw new RuntimeException("invalid compression algorithm"); case 107: throw new RuntimeException("Invalid file mime types"); default: throw new RuntimeException(message + " " + error); } } return activityId; } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } httpClient.close(); } }
From source file:com.lifetime.util.TimeOffUtil.java
protected static List<RepliconTimeOff> createTimeOffList(String response) throws ParseException { JSONParser parser = new JSONParser(); JSONObject jsonObject = (JSONObject) parser.parse(response); JSONObject d = (JSONObject) jsonObject.get("d"); JSONArray rows = (JSONArray) d.get("rows"); List<RepliconTimeOff> list = new ArrayList<RepliconTimeOff>(); for (Object row : rows) { JSONArray cells = (JSONArray) ((JSONObject) row).get("cells"); JSONObject owner = (JSONObject) cells.get(0); String employeeName = (String) owner.get("textValue"); JSONObject department = (JSONObject) cells.get(1); String departmentName = (String) department.get("textValue"); JSONObject ptoType = (JSONObject) cells.get(2); String ptoTypeName = (String) ptoType.get("textValue"); SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy"); JSONObject startDateJson = (JSONObject) cells.get(3); String startDateString = (String) startDateJson.get("textValue"); Date startDate = null;// w w w .ja v a 2 s . c o m try { startDate = dateFormat.parse(startDateString); } catch (java.text.ParseException e) { e.printStackTrace(); } JSONObject endDateJson = (JSONObject) cells.get(3); String endDateString = (String) endDateJson.get("textValue"); Date endDate = null; try { endDate = dateFormat.parse(endDateString); } catch (java.text.ParseException e) { e.printStackTrace(); } JSONObject approvedJson = (JSONObject) cells.get(5); String approvedString = (String) approvedJson.get("textValue"); boolean approved = approvedString.equals("Approved") ? true : false; RepliconTimeOff repliconTimeOff = new RepliconTimeOffImpl(employeeName, departmentName, startDate, endDate, ptoTypeName, approved); list.add(repliconTimeOff); } return list; }