List of usage examples for com.google.gson JsonObject get
public JsonElement get(String memberName)
From source file:serverSocket.java
License:Apache License
public JsonObject sortJson(JsonArray arr) { List<JsonObject> tmp = new ArrayList<JsonObject>(); for (JsonElement obj : arr) { tmp.add(obj.getAsJsonObject());//from w w w.j a v a 2 s . c o m } Collections.sort(tmp, new Comparator<JsonObject>() { @Override public int compare(JsonObject a, JsonObject b) { return (int) (a.get("epoch").getAsLong() - b.get("epoch").getAsLong()); } }); //classify by time slots long start = tmp.get(0).get("epoch").getAsLong(); //15 mins long slot = 15L * 60L; start += slot; JsonArray collection = new JsonArray(); JsonObject done = new JsonObject(); for (JsonObject obj : tmp) { if (obj.get("epoch").getAsLong() > start) { done.add(String.valueOf(start), collection); start = obj.get("epoch").getAsLong(); start += slot; collection = new JsonArray(); collection.add(obj); } else { collection.add(obj); } } return done; }
From source file:serverSocket.java
License:Apache License
@OnMessage public void incoming(String message) throws Exception { //System.out.println(message); JsonObject element = jsonParser.parse(message).getAsJsonObject(); //System.out.println(element); try {/* w ww .j a v a 2 s.c o m*/ String act = element.get("action").toString(); //critical: escape ", "a" -> a act = act.substring(1, act.length() - 1); switch (act) { case "ELAPSE": JsonObject obj = new JsonObject(); obj.add("elapse", list); sendMsg(session, obj.toString()); break; case "UpdateKeyWords": obj = new JsonObject(); obj.add("update", element.get("data")); //System.out.println(obj.toString()); broadcast(obj.toString()); break; case "geo_search": //System.out.println(element.get("lat").toString()+element.get("lng").toString()); sendMsg(session, geoQuery(element.get("lat").toString(), element.get("lng").toString())); break; case "key_search": //critical: escape string \" \" String keyw = element.get("keyword").toString(); keyw = keyw.substring(1, keyw.length() - 1); // ArrayList<String> kx = new ArrayList<String>(); // kx.add("key"); // kx.add("sentiment"); // list to json // query1.addProperty("fields", new Gson().toJson(kx)); sendMsg(session, queryKeyHistroy(keyw)); break; } } catch (NullPointerException e) { System.out.println(message); e.printStackTrace(); } // Never trust the client }
From source file:serverSocket.java
License:Apache License
public String geoQuery(String lat, String lng) { JsonObject query0 = new JsonObject(); query0.addProperty("lat", lat); query0.addProperty("lon", lng); JsonObject query1 = new JsonObject(); query1.add("location", query0); query1.addProperty("distance", "50km"); JsonObject query2 = new JsonObject(); query2.add("geo_distance", query1); JsonObject query5 = new JsonObject(); query5.add("filter", query2); JsonObject query6 = new JsonObject(); query6.add("filtered", query5); JsonObject query7 = new JsonObject(); query7.add("query", query6); String response = HttpRequest.post(esURL).send(query7.toString()).body(); JsonObject sr = jsonParser.parse(response).getAsJsonObject(); JsonArray asr = sr.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); JsonObject rb = new JsonObject(); rb.add("geo_res", asr); return rb.toString(); }
From source file:serverSocket.java
License:Apache License
public String queryKeyHistroy(String keyw) { JsonObject query1 = new JsonObject(); JsonObject query0 = new JsonObject(); query0.addProperty("key", keyw); //query0.addProperty("sentiment", 5); JsonObject query4 = new JsonObject(); query4.add("term", query0); query1.add("query", query4); query1.addProperty("from", 0); query1.addProperty("size", 10000); String response1 = HttpRequest.post(esURL1).send(query1.toString()).body(); JsonObject sr1 = jsonParser.parse(response1).getAsJsonObject(); JsonArray asr1 = sr1.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); JsonObject line = new JsonObject(); for (JsonElement x : asr1) { line.add(x.getAsJsonObject().get("_source").getAsJsonObject().get("tid").toString(), x.getAsJsonObject().get("_source")); }//from w w w .ja v a 2 s. c o m JsonObject res = new JsonObject(); res.add("update", line); return res.toString(); }
From source file:ExplorerPanel.java
License:Apache License
public void editWith(String editable) { try {/*from w w w. jav a 2 s. com*/ JsonObject editors = RunnerRepository.getEditors(); int length = editors.entrySet().size(); Iterator iter = editors.entrySet().iterator(); Entry entry; String[] vecresult; if (editors.get("DEFAULT") != null) { vecresult = new String[length - 1]; } else { vecresult = new String[length]; } int index = 0; for (int i = 0; i < length; i++) { entry = (Entry) iter.next(); if (entry.getKey().toString().equals("DEFAULT")) { continue; } vecresult[index] = entry.getKey().toString(); index++; } JComboBox jComboBox1 = new JComboBox(); JPanel p = getEditorsPanel(jComboBox1, vecresult); Object[] message = new Object[] { p }; int r = (Integer) CustomDialog.showDialog(p, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, tree, "Please select an editor", null); if (r == JOptionPane.OK_OPTION) { String ID = jComboBox1.getSelectedItem().toString(); String remotefilename = tree.getSelectionPath() .getPathComponent(tree.getSelectionPath().getPathCount() - 2) + "/" + tree.getSelectionPath().getLastPathComponent(); String localfilename = RunnerRepository.temp + RunnerRepository.getBar() + "Twister" + RunnerRepository.getBar() + tree.getSelectionPath().getLastPathComponent(); if (ID.equals("Embedded")) { openEmbeddedEditor(editable, remotefilename, localfilename); } else { File file2 = copyFileLocaly(remotefilename, localfilename); String execute = RunnerRepository.getEditors().get(ID).getAsString(); executeCommand(execute, localfilename); sendFileToServer(file2, remotefilename); file2.delete(); } } } catch (Exception e) { System.out .println("There was an error in opening editors " + "window, please check configuration file"); e.printStackTrace(); } }
From source file:PanicDetect.java
License:Apache License
private void listRegex() { try {/*from w ww . j a v a2 s . c om*/ String result = RunnerRepository.getRPCClient() .execute("panicDetectConfig", new Object[] { RunnerRepository.getUser(), "list" }).toString(); JsonElement jelement = new JsonParser().parse(result); JsonObject main = jelement.getAsJsonObject(); JsonObject regex = main.getAsJsonObject(RunnerRepository.getUser()); Iterator<Map.Entry<String, JsonElement>> iter = regex.entrySet().iterator(); while (iter.hasNext()) { Map.Entry<String, JsonElement> n = iter.next(); String id = n.getKey(); JsonElement content = n.getValue(); JsonObject ob = content.getAsJsonObject(); String exp = ob.get("expression").getAsString(); String en = ob.get("enabled").getAsString(); MyPanel panel = new MyPanel(exp, Boolean.parseBoolean(en), id); addPanel(panel); } } catch (Exception e) { e.printStackTrace(); } }
From source file:ClearCasePanel.java
License:Apache License
public void editWith(String editable, String ctfile, boolean save) { try {/* www .j a v a2 s . c o m*/ JsonObject editors = RunnerRepository.getEditors(); int length = editors.entrySet().size(); Iterator iter = editors.entrySet().iterator(); Entry entry; String[] vecresult; if (editors.get("DEFAULT") != null) { vecresult = new String[length - 1]; } else { vecresult = new String[length]; } int index = 0; for (int i = 0; i < length; i++) { entry = (Entry) iter.next(); if (entry.getKey().toString().equals("DEFAULT")) { continue; } vecresult[index] = entry.getKey().toString(); index++; } JComboBox jComboBox1 = new JComboBox(); JPanel p = getEditorsPanel(jComboBox1, vecresult); Object[] message = new Object[] { p }; int r = (Integer) CustomDialog.showDialog(p, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, tree, "Please select an editor", null); if (r == JOptionPane.OK_OPTION) { String ID = jComboBox1.getSelectedItem().toString(); String remotefilename = tree.getSelectionPath() .getPathComponent(tree.getSelectionPath().getPathCount() - 2) + "/" + tree.getSelectionPath().getLastPathComponent(); String localfilename = RunnerRepository.temp + RunnerRepository.getBar() + "Twister" + RunnerRepository.getBar() + tree.getSelectionPath().getLastPathComponent(); if (ID.equals("Embedded")) { openEmbeddedEditor(editable, remotefilename, localfilename, ctfile, save); } else { RunnerRepository.window.mainpanel.getP5().sendCommand("cp " + remotefilename + " ~"); RunnerRepository.window.mainpanel.getP5().readOutput(null); // RunnerRepository.window.mainpanel.getP5().sendCommand("cp "+remotefilename+" ~", false); // RunnerRepository.window.mainpanel.getP5().readOutput(true); File file2 = copyFileLocaly(editable, localfilename); RunnerRepository.window.mainpanel.getP5().sendCommand("rm -f ~/" + editable); RunnerRepository.window.mainpanel.getP5().readOutput(null); // RunnerRepository.window.mainpanel.getP5().sendCommand("rm -f ~/"+editable, false); // RunnerRepository.window.mainpanel.getP5().readOutput(true); String execute = RunnerRepository.getEditors().get(ID).getAsString(); executeCommand(execute, localfilename); sendFileToServer(file2, editable, ctfile); file2.delete(); } } } catch (Exception e) { System.out .println("There was an error in opening editors " + "window, please check configuration file"); e.printStackTrace(); } }
From source file:CompanyController.java
public static Object getCompanyById(Request request, Response response, CompaniesEditor compEdit) { HttpURLConnection con = null; try {/* w ww .j a v a 2 s. c om*/ int id = Integer.valueOf(request.params("id")); Company company = compEdit.get(id); if (company != null) { FinalJson json = new FinalJson(); String otherService = ""; try { String url = "http://owner:4321/people/" + id; URL obj = new URL(url); con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response1 = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response1.append(inputLine); } in.close(); otherService = response1.toString(); JsonObject jsonObject = new JsonParser().parse(otherService).getAsJsonObject(); String ownerName = jsonObject.get("name").getAsString(); String ownerSurname = jsonObject.get("surname").getAsString(); String ownerGender = jsonObject.get("gender").getAsString(); String ownerAddress = jsonObject.get("address").getAsString(); json.ownerAddress = ownerAddress; json.ownerGender = ownerGender; json.ownerName = ownerName; json.ownerSurname = ownerSurname; json.id = company.getId(); json.city = company.getCity(); json.name = company.getName(); json.phoneNumber = company.getPhoneNumber(); return json; } catch (Exception e) { return company; } } else { response.status(404); throw new Exception("404. Failed to find company by id: " + id + "!"); } } catch (Exception e) { return e.getMessage(); } finally { if (con != null) { con.disconnect(); } } }
From source file:JsonHubProtocol.java
License:Open Source License
@Override public HubMessage[] parseMessages(String payload) { String[] messages = payload.split(RECORD_SEPARATOR); List<HubMessage> hubMessages = new ArrayList<>(); for (String splitMessage : messages) { JsonObject jsonMessage = jsonParser.parse(splitMessage).getAsJsonObject(); HubMessageType messageType = HubMessageType.values()[jsonMessage.get("type").getAsInt() - 1]; switch (messageType) { case INVOCATION: //Invocation Message String target = jsonMessage.get("target").getAsString(); JsonElement args = jsonMessage.get("arguments"); hubMessages.add(new InvocationMessage(target, new Object[] { args })); break; case STREAM_ITEM: throw new UnsupportedOperationException("Support for streaming is not yet available"); case COMPLETION: //Don't care yet break; case STREAM_INVOCATION: //Don't care yet; throw new UnsupportedOperationException("Support for streaming is not yet available"); case CANCEL_INVOCATION: // Not tracking invocations yet break; case PING: //Ping hubMessages.add(new PingMessage()); break; case CLOSE: //Don't care yet; break; }//from w w w . j av a2 s .c o m } return hubMessages.toArray(new HubMessage[hubMessages.size()]); }
From source file:GeoLocation.java
public static void main(String args[]) { try {// w w w. j ava 2 s .c om URL myURL = new URL( "https://maps.googleapis.com/maps/api/geocode/json?latlng=17.451425,%2078.380957&key=AIzaSyDe_RBKl4DBGy6YCWRPXi9eQzYACt05Tr8"); URLConnection myURLConnection = myURL.openConnection(); myURLConnection.connect(); BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; while ((line = in.readLine()) != null) { sb.append(line + "\n"); } JsonElement json = new JsonParser().parse(sb.toString()); JsonObject j = json.getAsJsonObject(); JsonElement je = j.get("results"); JsonArray ja = je.getAsJsonArray(); JsonElement je1 = ja.get(1); JsonObject j1 = je1.getAsJsonObject(); JsonElement je2 = j1.get("formatted_address"); System.out.println(je2.getAsString()); } catch (MalformedURLException e) { // new URL() failed // ... } catch (IOException e) { // openConnection() failed // ... } }