List of usage examples for org.json.simple JSONObject toJSONString
public String toJSONString()
From source file:de.root1.kad.cvbackend.Test.java
public static void main(String[] args) { String ga = ""; String value = ""; JSONObject jsonResponse = new JSONObject(); JSONObject jsonData = new JSONObject(); jsonData.put(ga, value);//from w ww .j a v a 2 s . c o m jsonResponse.put("d", jsonData); jsonResponse.put("i", "1"); System.out.println(jsonResponse.toJSONString()); }
From source file:SentiWordNetDemoCode.java
public static void main(String[] args) throws IOException { if (args.length < 2) { System.err.println("Usage: java SentiWordNetDemoCode <pathToSentiWordNetFile>"); return;/*from w ww . jav a2 s . c om*/ } String pathToSWN = args[0]; SentiWordNetDemoCode sentiwordnet = new SentiWordNetDemoCode(pathToSWN); JSONObject js = new JSONObject(sentiwordnet.dictionary); FileWriter csv = null; try { csv = new FileWriter(args[1]); csv.write(js.toJSONString()); System.out.println("error"); } catch (Exception e) { e.printStackTrace(); } finally { if (csv != null) { csv.close(); } } // System.out.println("good#a "+sentiwordnet.extract("good", "a")); // System.out.println("bad#a "+sentiwordnet.extract("bad", "a")); // System.out.println("blue#a "+sentiwordnet.extract("blue", "a")); // System.out.println("blue#n "+sentiwordnet.extract("blue", "n")); }
From source file:app.WebSocketServer.java
public static void main(final String[] args) { Undertow server = Undertow.builder().addHttpListener(3030, "0.0.0.0") .setHandler(path().addPrefixPath("/ws", websocket(new WebSocketConnectionCallback() { @Override/* w w w . j a va2 s. co m*/ public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) { channels.add(channel); channel.getReceiveSetter().set(new AbstractReceiveListener() { @Override protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) { JSONObject msg = (JSONObject) JSONValue.parse(message.getData()); switch (msg.get("type").toString()) { case "broadcast": final String outbound = msg.toJSONString(); channels.forEach(gc -> WebSockets.sendText(outbound, gc, null)); msg.replace("type", "broadcastResult"); WebSockets.sendText(msg.toJSONString(), channel, null); break; case "echo": WebSockets.sendText(msg.toJSONString(), channel, null); break; default: System.out.println("Unknown message type"); } } }); channel.addCloseTask(new ChannelListener<WebSocketChannel>() { @Override public void handleEvent(WebSocketChannel channel) { channels.remove(channel); } }); channel.resumeReceives(); } }))).build(); server.start(); }
From source file:com.github.fhuss.kafka.streams.cep.demo.CEPStockKStreamsDemo.java
public static void main(String[] args) { Properties props = new Properties(); props.put(StreamsConfig.APPLICATION_ID_CONFIG, "streams-cep"); props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(StreamsConfig.ZOOKEEPER_CONNECT_CONFIG, "localhost:2181"); props.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, StockEventSerDe.class); props.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, StockEventSerDe.class); // setting offset reset to earliest so that we can re-run the demo code with the same pre-loaded data props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); // build query final Pattern<Object, StockEvent> pattern = new QueryBuilder<Object, StockEvent>().select() .where((k, v, ts, store) -> v.volume > 1000).<Long>fold("avg", (k, v, curr) -> v.price).then() .select().zeroOrMore().skipTillNextMatch() .where((k, v, ts, state) -> v.price > (long) state.get("avg")) .<Long>fold("avg", (k, v, curr) -> (curr + v.price) / 2) .<Long>fold("volume", (k, v, curr) -> v.volume).then().select().skipTillNextMatch() .where((k, v, ts, state) -> v.volume < 0.8 * state.getOrElse("volume", 0L)) .within(1, TimeUnit.HOURS).build(); KStreamBuilder builder = new KStreamBuilder(); CEPStream<Object, StockEvent> stream = new CEPStream<>(builder.stream("StockEvents")); KStream<Object, Sequence<Object, StockEvent>> stocks = stream.query("Stocks", pattern); stocks.mapValues(seq -> {// w ww . jav a 2 s . co m JSONObject json = new JSONObject(); seq.asMap().forEach((k, v) -> { JSONArray events = new JSONArray(); json.put(k, events); List<String> collect = v.stream().map(e -> e.value.name).collect(Collectors.toList()); Collections.reverse(collect); collect.forEach(e -> events.add(e)); }); return json.toJSONString(); }).through(null, Serdes.String(), "Matches").print(); //Use the topologyBuilder and streamingConfig to start the kafka streams process KafkaStreams streaming = new KafkaStreams(builder, props); //streaming.cleanUp(); streaming.start(); }
From source file:contractEditor.SPConfFileEditor.java
public static void main(String[] args) { JSONObject obj = new JSONObject(); ArrayList fileList = new ArrayList(); fileList.add("confSP" + File.separator + "HOST1.json"); fileList.add("confSP" + File.separator + "HOST2.json"); fileList.add("confSP" + File.separator + "HOST3.json"); fileList.add("confSP" + File.separator + "HOST4.json"); obj.put("contract_list_of_SP", fileList); try {//www . j a va 2 s . c o m FileWriter file = new FileWriter("confSP" + File.separator + "contractFileList.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); }
From source file:com.mysql.servlet.NewClass.java
public static void main(String[] args) { JSONObject headerFile = new JSONObject(); JSONArray listOfAttrs = new JSONArray(); listOfAttrs.add("id" + ""); listOfAttrs.add("name" + ""); listOfAttrs.add("age" + ""); listOfAttrs.add("salary" + ""); listOfAttrs.add("grade" + ""); headerFile.put("Employee", listOfAttrs); try {//from ww w .ja v a2 s . com // Writing to a file File file = new File("D:\\Employees.json"); file.createNewFile(); FileWriter fileWriter = new FileWriter(file); System.out.println("Writing JSON object to file"); System.out.println("-----------------------"); System.out.print(headerFile); fileWriter.write(headerFile.toJSONString()); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:jsonpractice.Jsonpractice.java
public static void main(String[] args) throws IOException { JSONObject countryObj = new JSONObject(); countryObj.put("Name", "India"); countryObj.put("Population", 1000000); JSONArray listOfStates = new JSONArray(); listOfStates.add("Madhya Pradesh"); listOfStates.add("Maharastra"); listOfStates.add("Tamil Nadu"); countryObj.put("States", listOfStates); try {/*from w w w . ja v a 2s . c o m*/ File file = new File("C:\\Users\\user\\Documents\\CountryJSONFile.json"); file.createNewFile(); try (FileWriter fileWriter = new FileWriter(file)) { System.out.println("Writing JSON OBJECT to file"); System.out.println("-----------------------------"); System.out.println(countryObj); fileWriter.write(countryObj.toJSONString()); fileWriter.flush(); } catch (IOException e) { } } catch (IOException e) { } }
From source file:cat.tv3.eng.rec.recomana.lupa.visualization.ClustersToJson.java
public static void main(String[] args) throws IOException { String host = args[0];//from www. ja v a 2 s.co m int port = Integer.parseInt(args[1]); Jedis jedis = new Jedis(host, port, 20000); // Cluster to binary tree visualitzation Map<String, String> attr_cluster = jedis.hgetAll("ClusterBinaryTree-Arrel"); String cluster_name = attr_cluster.get("cluster_ids_name"); JSONObject cluster; if (!cluster_name.equals("cluster_splited")) { cluster = new JSONObject(); cluster.put("name", "arrel"); } else { String id_left_centroid = attr_cluster.get("id_left_centroid"); String id_right_centroid = attr_cluster.get("id_right_centroid"); String hash_left = attr_cluster.get("hash_left"); String hash_right = attr_cluster.get("hash_right"); cluster = new JSONObject(); cluster.put("name", "arrel"); cluster.put("children", hashToJSONArrayRepresentationBinaryTree(id_left_centroid, hash_left, jedis, id_right_centroid, hash_right)); } jedis.disconnect(); Writer out = new BufferedWriter( new OutputStreamWriter(new FileOutputStream("data_toVisualize/cluster.json"), "UTF-8")); try { out.write(cluster.toJSONString()); } finally { out.close(); } }
From source file:com.opensoc.json.serialization.JSONKafkaSerializer.java
public static void main(String args[]) throws IOException { //String Input = "/home/kiran/git/opensoc-streaming/OpenSOC-Common/BroExampleOutput"; String Input = "/tmp/test"; BufferedReader reader = new BufferedReader(new FileReader(Input)); // String jsonString = // "{\"dns\":{\"ts\":[14.0,12,\"kiran\"],\"uid\":\"abullis@mail.csuchico.edu\",\"id.orig_h\":\"10.122.196.204\", \"endval\":null}}"; String jsonString = "";// reader.readLine(); JSONParser parser = new JSONParser(); JSONObject json = null; int count = 1; if (args.length > 0) count = Integer.parseInt(args[0]); //while ((jsonString = reader.readLine()) != null) jsonString = reader.readLine();//from w w w . ja va2 s . c o m { try { json = (JSONObject) parser.parse(jsonString); System.out.println(json); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } String jsonString2 = null; JSONKafkaSerializer ser = new JSONKafkaSerializer(); for (int i = 0; i < count; i++) { byte[] bytes = ser.toBytes(json); jsonString2 = ((JSONObject) ser.fromBytes(bytes)).toJSONString(); } System.out.println((jsonString2)); System.out.println(jsonString2.equalsIgnoreCase(json.toJSONString())); } }
From source file:contractEditor.contractHOST4.java
public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "HOST4"); obj.put("context", "VM-deployment"); //obj.put("Context", new Integer); HashMap serviceDescription = new HashMap(); serviceDescription.put("location", "France"); serviceDescription.put("certificate", "true"); serviceDescription.put("volume", "100_GB"); serviceDescription.put("price", "3_euro"); obj.put("serviceDescription", serviceDescription); HashMap gauranteeTerm = new HashMap(); gauranteeTerm.put("availability", "more_98_percentage"); obj.put("gauranteeTerm", gauranteeTerm); //Constraint1 ArrayList creationConstraint1 = new ArrayList(); ArrayList totalConstraint = new ArrayList(); totalConstraint.add(creationConstraint1); obj.put("creationConstraint", totalConstraint); try {//from ww w . jav a 2s. c o m FileWriter file = new FileWriter("confSP" + File.separator + "Host4.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); /* JSONParser parser = new JSONParser(); try { Object obj2 = parser.parse(new FileReader("confSP\\confHost1.json")); JSONObject jsonObject = (JSONObject) obj2; HashMap serviceDescription2=(HashMap) jsonObject.get("serviceDescription"); method.printHashMap(serviceDescription2); HashMap gauranteeTerm2=(HashMap) jsonObject.get("gauranteeTerm"); method.printHashMap(gauranteeTerm2); ArrayList creationConstraint=(ArrayList) jsonObject.get("creationConstraint"); method.printArrayList(creationConstraint); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } */ }