List of usage examples for org.json.simple JSONObject get
V get(Object key);
From source file:json.ReadFromFile.java
public static void main(String[] args) { JSONParser parser = new JSONParser(); try {//from w w w . ja v a 2s . c om Object obj = parser.parse(new FileReader(file)); JSONObject jsonObject = (JSONObject) obj; String countryName = jsonObject.get("Name") + "";//equivalent to jsonObject.get("Name").toString(); System.out.println("Name of Country: " + countryName); long population = (long) jsonObject.get("Population"); System.out.println("Population: " + population); System.out.println("Counties are:"); JSONArray listOfCounties = (JSONArray) jsonObject.get("Counties"); Iterator<String> iterator = listOfCounties.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } catch (IOException | ParseException e) { e.printStackTrace(); } }
From source file:jsonpractice.jsonpractice1.java
public static void main(String[] args) throws IOException, ParseException { JSONParser parser = new JSONParser(); try {/*from w w w . j av a 2s . c o m*/ Object obj = parser.parse(new FileReader("C:\\Users\\user\\Documents\\CountryJSONFile.json")); JSONObject jsonobject = (JSONObject) obj; String nameOfCountry = (String) jsonobject.get("Name"); System.out.println("Name of the Country: " + nameOfCountry); long population = (long) jsonobject.get("Population"); System.out.println("Population of the country is : " + population); System.out.println("States are: "); JSONArray listOfStates = (JSONArray) jsonobject.get("states"); Iterator<String> iterator = listOfStates.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } catch (FileNotFoundException e) { } catch (IOException e) { } catch (ParseException e) { } }
From source file:bonapetit.ParseJson1.java
public static void main(String[] args) { String url = "http://freemusicarchive.org/api/get/genres.json?api_key=60BLHNQCAOUFPIBZ&limit=2"; /*/*w w w. jav a 2 s . c om*/ * {"title":"Free Music Archive - Genres","message":"","errors":[],"total" : "161","total_pages":81,"page":1,"limit":"2", * "dataset": * [{"genre_id": "1","genre_parent_id":"38","genre_title":"Avant-Garde" ,"genre_handle": "Avant-Garde","genre_color":"#006666"}, * {"genre_id":"2","genre_parent_id" :null,"genre_title":"International","genre_handle":"International","genre_color":"#CC3300"}]} */ try { String genreJson = IOUtils.toString(new URL(url)); JSONObject genreJsonObject = (JSONObject) JSONValue.parseWithException(genreJson); // get the title System.out.println(genreJsonObject.get("title")); // get the data JSONArray genreArray = (JSONArray) genreJsonObject.get("dataset"); // get the first genre JSONObject firstGenre = (JSONObject) genreArray.get(0); System.out.println(firstGenre.get("genre_title")); } catch (IOException | ParseException e) { e.printStackTrace(); } }
From source file:MyTest.ParseJson.java
public static void main(String args[]) { try {// w w w . jav a 2 s. c o m FileReader reader = new FileReader( new File("data/NGS___RNA-seq_differential_expression_analysis-v1.ga")); System.out.println(reader); JSONParser parser = new JSONParser(); JSONObject jo = (JSONObject) parser.parse(reader); JSONObject jsteps = (JSONObject) jo.get("steps"); System.out.println(jsteps.size()); System.out.println("---------------------------------------------------"); for (int i = 0; i < jsteps.size(); i++) { JSONObject jstep_detail = (JSONObject) jsteps.get(String.valueOf(i)); getDetailInfo(jstep_detail); } } catch (FileNotFoundException ex) { Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex); } catch (ParseException ex) { Logger.getLogger(ParseJson.class.getName()).log(Level.SEVERE, null, ex); } }
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//from w w w .ja va2 s. c om 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.aj.hangman.HangmanReq.java
public static void main(String[] args) { HangmanDict dictionary = new HangmanDict(); try {//from ww w. j a v a 2 s . co m BufferedReader input = new BufferedReader(new InputStreamReader( new URL("http://gallows.hulu.com/play?code=gudehosa@usc.edu").openStream())); String info = input.readLine(); JSONParser parser = new JSONParser(); Object obj; try { obj = parser.parse(info); JSONObject retJson = (JSONObject) obj; TOKEN = (String) retJson.get("token"); STATUS = (String) retJson.get("status"); STATE = (String) retJson.get("state"); REM = (Long) retJson.get("remaining_guesses"); PREM = REM; System.out.println("State:: " + STATE); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } while ("ALIVE".equalsIgnoreCase(STATUS)) { // call make guess function, returns character guess = dictionary.makeGuess(STATE); System.out.println("Guessed:: " + guess); // call the url to update BufferedReader reInput = new BufferedReader( new InputStreamReader(new URL("http://gallows.hulu.com/play?code=gudehosa@usc.edu" + String.format("&token=%s&guess=%s", TOKEN, guess)).openStream())); // parse the url to get the updated value String reInfo = reInput.readLine(); JSONParser reParser = new JSONParser(); Object retObj = reParser.parse(reInfo); JSONObject retJson = (JSONObject) retObj; STATUS = (String) retJson.get("status"); STATE = (String) retJson.get("state"); REM = (Long) retJson.get("remaining_guesses"); System.out.println("State:: " + STATE); } if ("DEAD".equalsIgnoreCase(STATUS)) { // print lost System.out.println("You LOOSE: DEAD"); } else if ("FREE".equalsIgnoreCase(STATUS)) { // print free System.out.println("You WIN: FREE"); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:chat.jsonTest.java
/** * @param args the command line arguments */// w ww . j av a2s .com public static void main(String[] args) throws ParseException, IOException { // TODO code application logic here JSONParser parser = new JSONParser(); String fullPath = "userInfo.json"; //BufferedReader reader = new BufferedReader(new FileReader(fullPath)); Object obj = parser.parse(new FileReader("userInfo.json")); JSONObject jsonObject = (JSONObject) obj; /*while ((line = reader.readLine()) != null) { System.out.println(line); }*/ JSONArray lang = (JSONArray) jsonObject.get("userInfo"); Iterator i = lang.iterator(); // take each value from the json array separately while (i.hasNext()) { JSONObject innerObj = (JSONObject) i.next(); System.out .println("username " + innerObj.get("username") + " with password " + innerObj.get("password")); } }
From source file:com.github.artifactresolver.MavenDependencyDownloader.java
public static void main(String[] args) throws Exception { new MavenDependencyDownloader(); parseCommandLine(args);/* ww w .j ava 2 s.com*/ RepositorySystemHelper repoSystemHelper = new RepositorySystemHelper(localRepo); dependencyResolver = new DependencyResolver(repoSystemHelper); if (artifacts.isEmpty()) { JSONParser jsonParser = new JSONParser(); FileReader fileReader = new FileReader(new File(dependencyFile)); JSONArray jsonArray = (JSONArray) jsonParser.parse(fileReader); fileReader.close(); for (Object obj : jsonArray) { JSONObject jsonObj = (JSONObject) obj; String groupId = (String) jsonObj.get("groupId"); String artifactId = (String) jsonObj.get("artifactId"); String classifier = (String) jsonObj.get("classifier"); String extension = (String) jsonObj.get("extension"); String version = (String) jsonObj.get("version"); DefaultArtifact artifact = new DefaultArtifact(groupId, artifactId, classifier, extension, version); dependencyResolver.downloadDependencyTree(artifact, javadoc, sources); } } else { for (DefaultArtifact artifact : artifacts) { dependencyResolver.downloadDependencyTree(artifact, javadoc, sources); } } log.info("Artifacts downloaded to \"{}\". Finished. Thank you.", localRepo); }
From source file:it.avalz.opendaylight.controller.examples.JsonParser.java
public static void main(String[] args) { String s = "{\"ids\":[\"00:00:00:00:00:00:00:01\", \"00:00:00:00:00:00:00:02\"], \"width\":200, \"height\":100}"; JSONObject json = null; try {//from www . j a va2 s . c o m json = (JSONObject) new JSONParser().parse(s); } catch (ParseException ex) { ex.printStackTrace(); } System.out.println(json.get("ids")); JSONArray array = (JSONArray) json.get("ids"); System.out.println(array.get(0)); System.out.println(json.get("width")); System.out.println(json.get("height")); List<String> l = new ArrayList<String>(); l.add("\"DROP\""); l.add("\"OUTPUT=2\""); System.out.println(l); }
From source file:com.devdungeon.simplejson.DecodeJson.java
public static void main(String[] args) { try {// w w w.jav a2 s. com String jsonData = getJsonFromUrl("http://www.reddit.com/r/houston.json"); JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonData); // The whole JSON value //System.out.println(jsonObject); JSONObject data = (JSONObject) jsonObject.get("data"); // The data object //System.out.println(data); JSONArray children = (JSONArray) data.get("children"); // All children // for each child in children // get child("data") for (int i = 0; i < children.size(); i++) { JSONObject child = (JSONObject) children.get(i); //System.out.println(child); // The whole post object JSONObject childData = (JSONObject) child.get("data"); System.out.print(childData.get("title") + ": "); System.out.println(childData.get("url")); } } catch (ParseException ex) { ex.printStackTrace(); } catch (NullPointerException ex) { ex.printStackTrace(); } }