List of usage examples for org.json.simple JSONValue parse
public static Object parse(String s)
From source file:mediasearch.twitter.TwitterService.java
private String requestBearerToken() { HttpsURLConnection connection = basicAuthConnectionPost(TwitterData.OAUTH_TOKEN_URL); JSONObject obj = (JSONObject) JSONValue.parse(readResponse(connection)); if (obj != null) { String tokenType = (String) obj.get("token_type"); String token = (String) obj.get("access_token"); return ((tokenType.equals("bearer")) && (token != null)) ? token : ""; }// w w w. j a va2 s . c om if (connection != null) { connection.disconnect(); } return null; }
From source file:edu.rit.chrisbitler.ritcraft.slackintegration.rtm.UserList.java
/** * Query the slack api to get the list of users and their real names. This is done whenever we can't find an ID mapping, * and when the plugin loads//from w w w.j ava2 s . co m */ public static void queryUsers() { try { users.clear(); //Contact the slack api to get the user list HttpURLConnection conn = (HttpURLConnection) new URL( "https://www.slack.com/api/users.list?token=" + SlackIntegration.BOT_TOKEN).openConnection(); conn.connect(); JSONObject retVal = (JSONObject) JSONValue .parse(new InputStreamReader((InputStream) conn.getContent())); JSONArray members = (JSONArray) retVal.get("members"); //Loop through the members and add them to the map Iterator<JSONObject> iter = members.iterator(); while (iter.hasNext()) { JSONObject obj = iter.next(); JSONObject profile = (JSONObject) obj.get("profile"); String id = (String) obj.get("id"); String realName = (String) profile.get("real_name"); users.put(id, realName); } } catch (IOException e) { e.printStackTrace(); } }
From source file:ch.simas.jtoggl.Task.java
public Task(String jsonString) { JSONObject object = (JSONObject) JSONValue.parse(jsonString); this.id = (Long) object.get("id"); this.name = (String) object.get("name"); this.estimated_seconds = (Long) object.get("estimated_seconds"); this.is_active = (Boolean) object.get("active"); JSONObject workspaceObject = (JSONObject) object.get("workspace"); if (workspaceObject != null) { this.workspace = new Workspace(workspaceObject.toJSONString()); }//from w ww. j a v a 2s . c o m JSONObject projectObject = (JSONObject) object.get("project"); if (projectObject != null) { this.project = new Project(projectObject.toJSONString()); } JSONObject userObject = (JSONObject) object.get("user"); if (userObject != null) { this.user = new User(userObject.toJSONString()); } }
From source file:ch.simas.jtoggl.Project.java
public Project(String jsonString) { JSONObject object = (JSONObject) JSONValue.parse(jsonString); this.id = (Long) object.get("id"); this.name = (String) object.get("name"); this.billable = (Boolean) object.get("billable"); this.active = (Boolean) object.get("active"); this.is_private = (Boolean) object.get("is_private"); this.template = (Boolean) object.get("template"); this.cid = (Long) object.get("cid"); JSONObject workspaceObject = (JSONObject) object.get("workspace"); if (workspaceObject != null) { this.workspace = new Workspace(workspaceObject.toJSONString()); }//w w w . j a va 2 s . co m }
From source file:com.jgoetsch.eventtrader.source.parser.TwitterJSONMsgParser.java
@SuppressWarnings("rawtypes") @Override//from w ww. ja v a 2 s .com protected boolean parseContent(String content, String contentType, MsgHandler handler) throws MsgParseException { if (contentType == null || !contentType.split(";")[0].trim().equalsIgnoreCase("application/json")) throw new MsgParseException( "Attempt to parse non-JSON content type " + contentType + " with JSONMsgParser"); Object json = JSONValue.parse(content); if (json instanceof Map) parseStatus((Map) json, handler); else if (json instanceof List) { for (Object status : (List) json) { if (status instanceof Map) parseStatus((Map) status, handler); } } return true; }
From source file:com.storageroomapp.client.PageOfEntries.java
/** * Parses a String of json text and returns an PageOfEntries object. * It will correctly parse the PageOfEntries object if it is toplevel, * or also if nested in an 'array' key-value pair. * /*from ww w. jav a 2s. co m*/ * @param json the String with the json text * @return an PageOfEntries object, or null if the parsing failed */ static public PageOfEntries parseJSON(Collection parent, String json) { if (json == null) { return null; } JSONObject jsonObj = (JSONObject) JSONValue.parse(json); if (jsonObj == null) { return null; } JSONObject jsonArrayObj = (JSONObject) jsonObj.get("array"); if (jsonArrayObj != null) { jsonObj = jsonArrayObj; } return parseJSONObject(parent, jsonObj); }
From source file:com.sourav.apps.PiMessageFetcher.java
@Override public void run() { while (running) { try {/*w ww . j a v a 2 s . co m*/ String rawJSON = client.get().getText(); JSONArray messages = (JSONArray) JSONValue.parse(rawJSON); PiChatterWindow window = PiChatterWindow.getWindow(); for (int i = 0; i < messages.size(); i++) { JSONObject message = (JSONObject) messages.get(i); String from = (String) message.get("from"); String time = (String) message.get("time"); String body = (String) message.get("data"); body = PiCodec.decode(body); String str = String.format(" %s: %s\t (sent at: %s)", from, body, time); window.appendChat(str); } if (messages.size() > 0) { menuref.setNotification(); } } catch (IOException iox) { iox.printStackTrace(); } try { Thread.sleep(600); } catch (InterruptedException iox) { iox.printStackTrace(); } } }
From source file:de.root1.logiccollection.offLogicVocESP8266.java
@Override public void init() { this.tt = new TimerTask() { @Override/*from w ww. j a v a 2 s .co m*/ public void run() { try { Socket s = new Socket("nodemcu1", 44444); OutputStream out = s.getOutputStream(); InputStream in = s.getInputStream(); out.write("\n".getBytes()); out.flush(); InputStreamReader isr = new InputStreamReader(in); JSONObject data = (JSONObject) JSONValue.parse(isr); isr.close(); out.close(); int voc = Integer.parseInt(data.get("voc").toString()); int tvoc = Integer.parseInt(data.get("voc").toString()); int resistance = Integer.parseInt(data.get("resistance").toString()); int status = Integer.parseInt(data.get("status").toString()); log.info("voc={}, tvoc={} resistance={} status={}", new Object[] { voc, tvoc, resistance, status }); // write(ga, String.valueOf(voc)); } catch (IOException ex) { ex.printStackTrace(); } } }; setPA("1.1.203"); t.schedule(tt, 5000, 60000); log.info("VOC ESP8266 reader is running."); }
From source file:cc.vidr.datum.util.FreebaseUtils.java
/** * Return the list of JSON objects returned by the API for the given query. * /*from w w w . j a va 2 s .c o m*/ * @param path the API service path * @return the list of JSON objects * @throws IOException if there was an error communicating with the * server * @throws ParseException if the server returns malformed JSON */ @SuppressWarnings("unchecked") public static List<JSONObject> getResultList(String path) throws IOException, ParseException { URLConnection connection = new URL(endpoint + path).openConnection(); connection.connect(); InputStream stream = connection.getInputStream(); Reader reader = new InputStreamReader(stream); JSONObject o = (JSONObject) JSONValue.parse(reader); return (List<JSONObject>) o.get("result"); }
From source file:com.jgoetsch.eventtrader.test.ProfidingMsgParserTest.java
@SuppressWarnings("rawtypes") public void testCommentary() throws Exception { msgParser.parseData("commentary", (Map) JSONValue.parse( "{\"newsletter\":3,\"date\":1344264740426,\"username\":\"timothysykes\",\"image\":\"http://a1.twimg.com/profile_images/1166026278/TimCover1_normal.jpg\",\"msg\":\"Message boards were speculating early in the year about their ability to manufacture iPad tablet screens, I'm gonna listen to conference call now and see if they confirmed it, then this could REALLY run, otherwise probly just gonna sell for a small gain \",\"msgId\":27889}"), new MsgHandler() { public boolean newMsg(Msg msg) { Assert.assertEquals(Msg.class, msg.getClass()); Assert.assertEquals("timothysykes", msg.getSourceName()); Assert.assertEquals("http://a1.twimg.com/profile_images/1166026278/TimCover1_normal.jpg", msg.getImageUrl()); Assert.assertEquals(1344264740426L, msg.getDate().toDate().getTime()); return false; }/*from w w w . ja v a2s. c om*/ }); // a classic one for sure msgParser.parseData("commentary", (Map) JSONValue.parse( "{\"newsletter\":3,\"date\":1344347965675,\"username\":\"timothysykes\",\"image\":\"http://a1.twimg.com/profile_images/1166026278/TimCover1_normal.jpg\",\"msg\":\"I'm pissed at your laziness and pissed at myself for not following through on my CRMB buy idea int he 2.80s, now 3.50+, what a waste we all are\",\"msgId\":28006}"), new MsgHandler() { public boolean newMsg(Msg msg) { Assert.assertEquals(Msg.class, msg.getClass()); Assert.assertEquals("timothysykes", msg.getSourceName()); Assert.assertEquals(1344347965675L, msg.getDate().toDate().getTime()); Assert.assertTrue( msg.getMessage().startsWith("I'm pissed at your laziness and pissed at myself")); return false; } }); }