List of usage examples for org.json.simple.parser JSONParser JSONParser
JSONParser
From source file:com.opensoc.parsing.test.BasicLancopeParserTest.java
/** * Test method for {@link com.opensoc.parsing.parsers.BasicLancopeParser#parse(byte[])}. * @throws Exception // w w w. j av a 2s . c om * @throws IOException */ public void testParse() throws IOException, Exception { for (String inputString : getInputStrings()) { JSONObject parsed = parser.parse(inputString.getBytes()); assertNotNull(parsed); System.out.println(parsed); JSONParser parser = new JSONParser(); Map<?, ?> json = null; try { json = (Map<?, ?>) parser.parse(parsed.toJSONString()); assertEquals(true, validateJsonData(super.getSchemaJsonString(), json.toString())); } catch (ParseException e) { e.printStackTrace(); } } }
From source file:Json.JsonWrite.java
public void jsonRead(String patchName) throws IOException, ParseException { JSONParser parser = new JSONParser(); JSONObject object = (JSONObject) parser.parse(new FileReader(patchName)); this.patch = (String) object.get("patch"); this.page = (String) object.get("page"); this.eddWay = (String) object.get("eddWay"); this.proffName = (String) object.get("proffName"); this.YStart = (String) object.get("yearStart"); this.YEnd = (String) object.get("yearEnd"); this.EddName = (String) object.get("eddName"); //System.out.println("Json.JsonWrite.jsonRead()"); }
From source file:com.gti.redirects.Redirects.RedirectStorage.java
private JSONArray parseFile() throws IOException, ParseException { JSONParser parser = new JSONParser(); return (JSONArray) parser.parse(new FileReader(jsonFile.getPath())); }
From source file:com.stratio.es.ESJavaRDDFT.java
/** * Imports dataset//from w w w . ja va2s.co 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:HostController.java
@RequestMapping("/hosts") public @ResponseBody Host host( @RequestParam(value = "authentication", required = false, defaultValue = "") String authentication) throws FileNotFoundException, UnsupportedEncodingException, IOException { Properties props = new Properties(); FileInputStream fis = new FileInputStream("properties.xml"); //loading properites from properties file props.loadFromXML(fis);//from w ww . ja v a2 s. c om String server_ip = props.getProperty("server_ip"); String ZABBIX_API_URL = "http://" + server_ip + "/api_jsonrpc.php"; // 1.2.3.4 is your zabbix_server_ip HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(ZABBIX_API_URL); // content-type is controlled in api_jsonrpc.php, so set it like this putMethod.setRequestHeader("Content-Type", "application/json-rpc"); // create json object for apiinfo.version JSONParser parser = new JSONParser(); JSONObject jsonObj = new JSONObject(); JSONArray list = new JSONArray(); jsonObj.put("jsonrpc", "2.0"); jsonObj.put("method", "host.get"); JSONObject params = new JSONObject(); params.put("output", "extend"); jsonObj.put("params", params); jsonObj.put("auth", authentication);// todo jsonObj.put("id", new Integer(1)); putMethod.setRequestBody(jsonObj.toString()); // put the json object as input stream into request body String loginResponse = ""; try { client.executeMethod(putMethod); // send to request to the zabbix api loginResponse = putMethod.getResponseBodyAsString(); // read the result of the response Object obj = parser.parse(loginResponse); JSONObject obj2 = (JSONObject) obj; String jsonrpc = (String) obj2.get("jsonrpc"); JSONArray array = (JSONArray) obj2.get("result"); for (int i = 0; i < array.size(); i++) { JSONObject tobj = (JSONObject) array.get(i); JSONObject objret = new JSONObject(); objret.put("hostid", tobj.get("hostid")); objret.put("hostName", tobj.get("host")); list.add(objret); } return new Host(list); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException pe) { pe.printStackTrace(); } return new Host( "Error: please provide the appropriate input parameters of the metric you are looking for its corresponding monitoring data:"); }
From source file:com.github.lgi2p.obirs.utils.JSONConverter.java
public static ObirsQuery parseObirsJSONQuery(SM_Engine engine, String jsonQuery) throws ParseException, Exception { logger.info("parsing query: " + jsonQuery); URIFactory factory = URIFactoryMemory.getSingleton(); jsonQuery = jsonQuery.replace("\n", ""); jsonQuery = jsonQuery.replace("\r", ""); JSONParser parser = new JSONParser(); Object obj = parser.parse(jsonQuery); JSONObject jsonObject = (JSONObject) obj; String aggregator = (String) jsonObject.get("aggregator"); String similarityMeasure = (String) jsonObject.get("similarityMeasure"); Double aggregatorParameter;//from www.ja v a 2s .c o m if (jsonObject.get("aggregatorParameter") != null) { aggregatorParameter = Double.parseDouble(jsonObject.get("aggregatorParameter").toString()); } else { aggregatorParameter = 2.0; } Double scoreThreshold = (Double) jsonObject.get("scoreThreshold"); Integer numberOfResults = (Integer) jsonObject.get("numberOfResults"); JSONArray concepts = (JSONArray) jsonObject.get("concepts"); Map<URI, Double> conceptsMap = new HashMap<URI, Double>(); ObirsQuery query = new ObirsQuery(conceptsMap, aggregator, similarityMeasure, aggregatorParameter, scoreThreshold, numberOfResults); if (concepts != null) { Iterator<JSONObject> iterator = concepts.iterator(); while (iterator.hasNext()) { JSONObject concept = iterator.next(); Double weight = Double.parseDouble(concept.get("weight").toString()); Object uriString = concept.get("uri"); if (uriString == null) { throw new SLIB_Ex_Critic("Error a URI must be specified for each query concept"); } URI uri = factory.getURI(uriString.toString()); if (engine.getGraph().containsVertex(uri)) { query.addConcept(uri, weight); } else { throw new Exception("concept associated to URI '" + uri + "' does not exists..."); } } } return query; }
From source file:mas.MAS_TOP_PAPERS.java
public static String getConferenceName(int id) { String url = "https://api.datamarket.azure.com/MRC/MicrosoftAcademic/v2/Conference?$filter=ID%20eq%20" + id + "&$format=json"; while (true) { try {/*ww w. j ava 2s . c om*/ StringBuilder csv_str = new StringBuilder(); final String json = getData2(url, 0); JSONParser parser = new JSONParser(); JSONObject jsonObj = (JSONObject) parser.parse(json); final JSONObject dObj = (JSONObject) jsonObj.get("d"); final JSONArray results = (JSONArray) dObj.get("results"); if (results.size() == 0) { System.out.println("results is Empty, break."); break; } else { // System.out.println("Conf: results# = " + results.size()); for (Object conf : results) { JSONObject confObj = (JSONObject) conf; String shortName = normalized((String) confObj.get("ShortName")); if (!shortName.equals("")) { return shortName; } else { String fullName = normalized((String) confObj.get("FullName")); return fullName; } } } // System.out.println("json= " + jsonObj); } catch (ParseException ex) { System.out.println(ex.getMessage() + " Cause: " + ex.getCause()); Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex); try { Thread.sleep(5000L); } catch (InterruptedException ex1) { Logger.getLogger(MAS_TOP_PAPERS.class.getName()).log(Level.SEVERE, null, ex1); } } } return null; }
From source file:me.bobbyallen.jlikkle.jLikkle.java
/** * Decodes the JSON data section to a usable 'stats' JSONObject * * @return JSONObject//from w w w.j a va 2 s . c o m * @throws ParseException */ private JSONObject responseDecodeStats(JSONObject dataObject) throws ParseException { JSONParser jsonParser = new JSONParser(); JSONObject statsObject = (JSONObject) jsonParser.parse(dataObject.get("stats").toString()); // The 'data.stats' section. return statsObject; }
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method that returns an {@link Entity} from a message, dealing woth different {@link MessageType}. * //w w w . j a va 2 s .co m * @param position - possible values are 1 or 2, returning the first or the second {@link Entity} contained in the message. * position = 2 makes sense only in case of {@link MessageType#PROXIMITY_UPDATE}. In case of other {@link MessageType} only * position = 1 can be passed. */ public static Entity getEntityFromMessage(int position, String message) { JSONObject jsonMsg; JSONParser parser = new JSONParser(); MessageType type = getMsgType(message); if (position == 1) { switch (type) { case PROXIMITY_UPDATE: try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY_1); return createEntity(type, jsonEntity); } catch (ParseException e) { e.printStackTrace(); } break; case PROPERTIES_UPDATE: case PROX_BEACONS: case SYNC_RESP: case CHECK_IN: case CHECK_OUT: try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY); return createEntity(type, jsonEntity); } catch (ParseException e) { e.printStackTrace(); } default: return null; } return null; } if (position == 2) { if (type.ordinal() == MessageType.PROXIMITY_UPDATE.ordinal()) { try { jsonMsg = (JSONObject) parser.parse(message); JSONObject msg = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); JSONObject jsonEntity = (JSONObject) msg.get(JsonStrings.ENTITY_2); return createEntity(type, jsonEntity); } catch (ParseException e) { e.printStackTrace(); } } return null; } return null; }