List of usage examples for org.json.simple.parser JSONParser JSONParser
JSONParser
From source file:it.polimi.geinterface.network.MessageUtils.java
/** * Method that return the sender {@link Entity} identifier from a message *//*from ww w . j av a2s. c o m*/ public static String getSenderID(String msgJson) { JSONParser parser = new JSONParser(); JSONObject jsonMsg; String senderID = ""; try { jsonMsg = (JSONObject) parser.parse(msgJson); JSONObject message = (JSONObject) jsonMsg.get(JsonStrings.MESSAGE); senderID = (String) message.get(JsonStrings.SENDER); return senderID; } catch (ParseException e) { e.printStackTrace(); } return null; }
From source file:com.AandC.Power.MainActivity.java
void ReviveJson() { if (Files.getFiles()[1].exists()) { try {//from ww w . java 2 s. com JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader(Files.FileSys[1])); JSONObject jsonObject = (JSONObject) obj; UserInfo.setName(jsonObject.get("name").toString()); UserInfo.setAge(Integer.parseInt(jsonObject.get("age").toString())); UserInfo.setDen(jsonObject.get("den").toString()); UserInfo.Update(); } catch (Exception e) { e.printStackTrace(); (new MsgBox("Problem", "There was a problem loading your information. " + "You will have to restart", this)) .Show(); } } }
From source file:csparql_shim.StdinStream.java
/** * start listening to stdin and forwarding to csparql * stream in defined windows to allow comparison with cqels *///from w ww . j a v a 2 s .c o m @Override public void run() { try { JSONParser parser = new JSONParser(); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line; int windowcount = 1; //loop for streaming in data while ((line = reader.readLine()) != null && !stop) { try { Object obj = parser.parse(line); JSONArray array = (JSONArray) obj; //stream the triple final RdfQuadruple q = new RdfQuadruple((String) array.get(0), (String) array.get(1), (String) array.get(2), System.currentTimeMillis()); this.put(q); System.out.println("triple sent at: " + System.currentTimeMillis()); } catch (ParseException pe) { System.err.println("Error when parsing input, incorrect JSON."); } } } catch (IOException e) { e.printStackTrace(); } }
From source file:me.wxwsk8er.Knapsack.Configuration.JSONConfiguration.java
private void parse() { try {/*from ww w. j av a2 s . c om*/ this.parser = new JSONParser(); this.jData = parser.parse(new FileReader(filePath)); this.jObject = (JSONObject) jData; } catch (Exception e) { e.printStackTrace(); } }
From source file:edu.ncsu.epc.models.Yelp.java
public ArrayList<YelpInfo> getYelpItems(String response) { // Update tokens here from Yelp developers site, Manage API access. int i = 0;// w ww.j a va2 s. c o m ArrayList<YelpInfo> list = new ArrayList<YelpInfo>(); YelpInfo yelpInfo; //System.out.println(response); try { JSONObject json = (JSONObject) new JSONParser().parse(response); JSONArray jsonArray = (JSONArray) new JSONParser().parse(json.get("businesses").toString()); //System.out.println(json.keySet()); //JSONObject json1 = (JSONObject) new JSONParser().parse(jsonArray.get(3).toString()); //System.out.println(json1.keySet()); while (i < jsonArray.size()) { yelpInfo = new YelpInfo(); JSONObject json1 = (JSONObject) new JSONParser().parse(jsonArray.get(i).toString()); i++; yelpInfo.rating = Double.parseDouble(json1.get("rating").toString()); JSONObject json2 = (JSONObject) new JSONParser().parse(json1.get("location").toString()); JSONArray jsonArray2 = (JSONArray) new JSONParser().parse(json2.get("address").toString()); if (jsonArray2.size() == 0) continue; yelpInfo.address = jsonArray2.get(0).toString(); // if(yelpInfo.address.length()==2) continue; // yelpInfo.address = yelpInfo.address.substring(1, yelpInfo.address.length()-1); // yelpInfo.address = yelpInfo.address.substring(1, yelpInfo.address.length()-1); yelpInfo.address += "," + json2.get("city") + "," + json2.get("state_code"); list.add(yelpInfo); } } catch (Exception e) { e.printStackTrace(); } return list; // Iterator<String> iterator = jsonArray.iterator(); // while (iterator.hasNext()) { // String str = (String)iterator.next(); // System.out.println(str); // JSONObject json1 = (JSONObject) new JSONParser().parse(iterator.next().toString()); // System.out.println(json1.keySet()); // } }
From source file:forumbox.EditPost.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request//from w ww.j av a 2 s .c o m * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = request.getRequestURI(); String[] urlpost = url.split("/"); String idPost = urlpost[3]; JSONParser passPost = new JSONParser(); JSONObject post; try { if (idPost != null) { Object objPost = passPost.parse(new FileReader( "/home/dinalidabarera/NetBeansProjects/NewFolder/forum4/admin01/post/" + idPost + ".json")); post = (JSONObject) objPost; String postuser = post.get("username").toString(); String posttitle = post.get("title").toString(); String postdescription = post.get("description").toString(); /* JSONArray array = (JSONArray) post.get("comments"); ArrayList<String> list; list = new ArrayList<>(); Iterator<String> iterator = array.iterator(); while (iterator.hasNext()) { list.add(iterator.next()); } int listsz = list.size(); */ /* String[] comments = new String[listsz]; for(int i=0;i<listsz;i++){ comments[i]=list.get(i); }*/ request.setAttribute("title", posttitle); request.setAttribute("description", postdescription); request.setAttribute("username", postuser); // request.setAttribute("comments",comments); request.setAttribute("id", idPost); request.getRequestDispatcher("/edit.jsp").forward(request, response); } } catch (Exception e) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(e); } }
From source file:io.github.casnix.spawnerdropper.SpawnerStack.java
public static boolean TakeSpawnerOutOfService(String spawnerType) { // Read ./SpawnerDropper.SpawnerStack.json into a string // get the value of JSON->{spawnerType} and subtract 1 to it unless it is 0. // If it's zero, return false // Write file back to disk try {// ww w . j a va 2 s . c om // Read entire ./SpawnerDropper.SpawnerStack.json into a string String spawnerStack = new String( Files.readAllBytes(Paths.get("./plugins/SpawnerDropper/SpawnerStack.json"))); // get the value of JSON->{spawnerType} JSONParser parser = new JSONParser(); Object obj = parser.parse(spawnerStack); JSONObject jsonObj = (JSONObject) obj; long numberInService = (Long) jsonObj.get(spawnerType); if (numberInService <= 0) { return false; } else { // System.out.println("[SD DBG MSG] TSOOS numberInServer("+numberInService+")"); numberInService -= 1; jsonObj.put(spawnerType, new Long(numberInService)); FileWriter file = new FileWriter("./plugins/SpawnerDropper/SpawnerStack.json"); file.write(jsonObj.toJSONString()); file.flush(); file.close(); return true; } } catch (ParseException e) { Bukkit.getLogger().warning("[SpawnerDropper] Caught ParseException in TakeSpawnerOutOfService(String)"); e.printStackTrace(); return false; } catch (FileNotFoundException e) { Bukkit.getLogger() .warning("[SpawnerDropper] Could not find ./plugins/SpawnerDropper/SpawnerStack.json"); e.printStackTrace(); return false; } catch (IOException e) { Bukkit.getLogger().warning("[SpawnerDropper] Caught IOException in TakeSpawnerOutOfService(String)"); e.printStackTrace(); return false; } }
From source file:com.marklogic.tableauextract.ExtractFromJSON.java
/** * Read JSON output from MarkLogic REST extension or *.xqy file and insert * the output into a Tabeleau table// w w w. j a va2 s .c o m * * @param table * @throws TableauException */ private static void insertData(Table table) throws TableauException, FileNotFoundException, ParseException, IOException { TableDefinition tableDef = table.getTableDefinition(); Row row = new Row(tableDef); URL url = new URL("http://localhost:8060/json.xqy"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(bufferedReader); JSONArray claims = (JSONArray) jsonObject.get("claims"); @SuppressWarnings("unchecked") Iterator<JSONObject> i = claims.iterator(); while (i.hasNext()) { JSONObject innerObject = i.next(); String idString = (String) innerObject.get("id"); row.setInteger(0, Integer.parseInt(idString.substring(0, 6))); row.setCharString(1, (String) innerObject.get("ssn")); row.setCharString(2, (String) innerObject.get("type")); String payString = (String) innerObject.get("payment_amount"); if (payString == null || payString.isEmpty()) payString = "0.0"; row.setDouble(3, Double.parseDouble(payString)); String dtString = (String) innerObject.get("claim_date"); if (dtString == null || dtString.isEmpty()) dtString = "1999-01-01"; LocalDate claimDate = (LocalDate.parse(dtString)); row.setDate(4, claimDate.getYear(), claimDate.getMonthValue(), claimDate.getDayOfMonth()); table.insert(row); } /* row.setDateTime( 0, 2012, 7, 3, 11, 40, 12, 4550); // Purchased row.setCharString(1, "Beans"); // Product row.setString( 2, "uniBeans"); // uProduct row.setDouble( 3, 1.08); // Price row.setDate( 6, 2029, 1, 1); // Expiration date row.setCharString(7, "Bohnen"); // Produkt for ( int i = 0; i < 10; ++i ) { row.setInteger(4, i * 10); // Quantity row.setBoolean(5, i % 2 == 1); // Taxed } */ }
From source file:org.kitodo.data.elasticsearch.index.type.UserGroupTypeTest.java
@Test public void shouldCreateDocument() throws Exception { UserGroupType userGroupType = new UserGroupType(); JSONParser parser = new JSONParser(); UserGroup userGroup = prepareData().get(0); HttpEntity document = userGroupType.createDocument(userGroup); JSONObject actual = (JSONObject) parser.parse(EntityUtils.toString(document)); JSONObject expected = (JSONObject) parser .parse("{\"title\":\"Administrator\",\"permission\":1," + "\"users\":[{\"id\":1},{\"id\":2}]}"); assertEquals("UserGroup JSONObject doesn't match to given JSONObject!", expected, actual); userGroup = prepareData().get(1);//from w w w . j a v a2 s .c o m document = userGroupType.createDocument(userGroup); actual = (JSONObject) parser.parse(EntityUtils.toString(document)); expected = (JSONObject) parser.parse("{\"title\":\"Random\",\"permission\":4,\"users\":[]}"); assertEquals("UserGroup JSONObject doesn't match to given JSONObject!", expected, actual); }
From source file:cloudnet.weather.data.forecastio.FormatConverter.java
private void aggregateData(String inputDir) { LOGGER.info("read weather data..."); File path = new File(inputDir); JSONParser parser = new JSONParser(); for (File cityDir : path.listFiles()) { // If not a dir; next file in data path if (!cityDir.isDirectory()) { continue; }/* w w w . j a v a2s. c o m*/ LOGGER.trace(String.format("Processing %s...", cityDir.getAbsolutePath())); String[] extensions = { "json" }; List<File> fileList = new ArrayList<>(FileUtils.listFiles(cityDir, extensions, false)); for (File file : fileList) { try { try (Reader reader = new BufferedReader(new FileReader(file))) { JSONObject jsonObject = (JSONObject) parser.parse(reader); String timezone = (String) jsonObject.get("timezone"); locationTimeZone.put(cityDir.getName(), timezone); Number currentTemp = (Number) ((JSONObject) jsonObject.get("currently")).get("temperature"); Number currentTime = (Number) ((JSONObject) jsonObject.get("currently")).get("time"); JSONObject hourlyForecast = (JSONObject) jsonObject.get("hourly"); JSONArray data = (JSONArray) hourlyForecast.get("data"); if (currentTemp != null) { addWeatherdata(currentTime.longValue(), cityDir.getName(), currentTemp.doubleValue()); } for (Object o : data) { JSONObject oo = (JSONObject) o; Number time = (Number) oo.get("time"); Number temp = (Number) oo.get("temperature"); long timeDiff = time.longValue() - currentTime.longValue(); if (temp != null) { addWeatherdata(currentTime.longValue() + timeDiff, cityDir.getName(), temp.doubleValue()); } } } } catch (IOException | ParseException e) { LOGGER.error("aggregateData", e); } } } }