// parse and set params public void setParams(String paramString) { if (paramString == null || paramString.isEmpty()) return; JsonReader jsonReader = null; try { jsonReader = javax.json.Json.createReader(new java.io.ByteArrayInputStream(paramString.getBytes())); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); String thresholdString = jsonObject.getString("threshold", null); if (thresholdString != null && !thresholdString.isEmpty()) try { threshold = Float.parseFloat(thresholdString); } catch (Exception ex) { } JsonArray paramList = jsonObject.getJsonArray("params"); if (paramList != null) { int mlen = paramList.size(); for (int i = 0; i < mlen; i++) { JsonObject mobj = paramList.getJsonObject(i); params.put(mobj.getString("name"), mobj.getString("value")); } } } catch (Exception ex) { logger.log(Level.WARNING, "Error to parse alert subscription params: " + paramString, ex); } }
private void readJsonStream(InputStream istream, JsonObjectCallback callback) throws IOException, UnsupportedEncodingException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int count; while ((count = istream.read(buffer)) > 0) { baos.write(buffer, 0, count); String s = new String(baos.toByteArray(), "UTF-8"); JsonReader reader = Json.createReader(new StringReader(s)); JsonObject json = null; try { json = reader.readObject(); } catch (JsonParsingException e) { // just ignore and continue continue; } baos.close(); baos = new ByteArrayOutputStream(); callback.callback(json); } }
/** * Parse a json message collection to create a Message * * @param arg json collection * @return Message */ public static Message readMessage(String arg) { JsonReader jsonReader = Json.createReader(new StringReader(arg)); JsonObject msgValues = jsonReader.readObject(); jsonReader.close(); if (null == msgValues.getJsonArray(ATTACHMENTS)) { return new Message( msgValues.getString(ID), msgValues.getString(SENDER), msgValues.getString(TOPIC), msgValues.getJsonNumber(TIMESTAMP).longValue(), msgValues.getString(CONTENT)); } else { JsonArray attachmentsJson = msgValues.getJsonArray(ATTACHMENTS); Byte[] attachmentByte = new Byte[attachmentsJson.size()]; for (int i = 0; i < attachmentByte.length; i++) { attachmentByte[i] = Byte.valueOf(attachmentsJson.getString(i)); } return new Message( msgValues.getString(ID), msgValues.getString(SENDER), msgValues.getString(TOPIC), msgValues.getJsonNumber(TIMESTAMP).longValue(), msgValues.getString(CONTENT), attachmentByte); } }
@Override public Photo readFrom( Class<Photo> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { try (JsonReader in = Json.createReader(entityStream)) { JsonObject jsonPhoto = in.readObject(); Photo p = new Photo(); p.setId(jsonPhoto.getInt("id")); p.setTitle(jsonPhoto.getString("title", null)); p.setUrl(jsonPhoto.getString("url", null)); p.setuComment(jsonPhoto.getString("uComment", null)); p.setDate(new Date(jsonPhoto.getJsonNumber("date").longValue())); User u = em.find(User.class, jsonPhoto.getString("uId")); p.setUser(u); return p; } catch (JsonException | ClassCastException ex) { throw new BadRequestException("Ongeldige JSON invoer"); } }
private static void readJsonFile() { try (JsonReader jsonReader = Json.createReader( new FileReader( Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) { JsonStructure jsonStructure = jsonReader.read(); JsonValue.ValueType valueType = jsonStructure.getValueType(); if (valueType == JsonValue.ValueType.OBJECT) { JsonObject jsonObject = (JsonObject) jsonStructure; JsonValue firstName = jsonObject.get("firstName"); LOGGER.info("firstName=" + firstName); JsonValue emailAddresses = jsonObject.get("phoneNumbers"); if (emailAddresses.getValueType() == JsonValue.ValueType.ARRAY) { JsonArray jsonArray = (JsonArray) emailAddresses; for (int i = 0; i < jsonArray.size(); i++) { JsonValue jsonValue = jsonArray.get(i); LOGGER.info("emailAddress(" + i + "): " + jsonValue); } } } else { LOGGER.warning("First object is not of type " + JsonValue.ValueType.OBJECT); } } catch (FileNotFoundException e) { LOGGER.severe("Failed to open file: " + e.getMessage()); } }
public JsonObject processInput(byte[] input) { ByteArrayInputStream byteInStream = new ByteArrayInputStream(input); JsonReader jsonReader = Json.createReader(byteInStream); JsonObject ob = jsonReader.readObject(); PieLogger.info(this.getClass(), String.format("ConnectionText: %s", ob.toString())); return ob; }
@Override public List<Employee> readEmployees(String infile) { List<Employee> emps = new ArrayList<>(); Employee emp; try (InputStream is = new FileInputStream(infile); JsonReader reader = Json.createReader(is)) { JsonObject obj = reader.readObject(); JsonArray empArr = obj.getJsonArray("Employees"); for (JsonObject empObj : empArr.getValuesAs(JsonObject.class)) { emp = new Employee(); emp.setId(empObj.getInt("ID", -1)); emp.setFirstName(empObj.getString("FirstName", null)); emp.setLastName(empObj.getString("LastName", null)); emp.setDepartmentNumber(empObj.getInt("DepartmentNumber", -1)); emp.setLocationId(empObj.getInt("LocationID", -1)); if (emp.getId() > 0 && emp.getFirstName() != null && emp.getLastName() != null) emps.add(emp); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JsonParsingException jpe) { System.err.println("Json file is malformed."); System.err.println(jpe.getMessage()); System.err.println(jpe.getStackTrace()); } return emps; }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("USAGE : java -jar GoEuroTest.jar <CITY_NAME>"); System.exit(1); } String urlStr = "http://api.goeuro.com/api/v2/position/suggest/en/"; urlStr = urlStr + URLEncoder.encode(args[0], "UTF-8"); URL url = new URL(urlStr); String lines = ""; String fileHeader = "_id,name,type,latitude,longitude\r\n"; // out csv file header lines += fileHeader; try { InputStream inputStream = url.openStream(); JsonReader jsonReader = Json.createReader(inputStream); JsonArray results = jsonReader.readArray(); if (results.isEmpty()) { System.out.println("INFO: City '" + args[0] + "' returned 0 results."); return; } for (JsonObject result : results.getValuesAs(JsonObject.class)) { lines += result.getJsonNumber("_id") + ","; lines += result.getString("name") + ","; lines += result.getString("type") + ","; lines += result.getJsonObject("geo_position").getJsonNumber("latitude") + ","; lines += result.getJsonObject("geo_position").getJsonNumber("longitude") + "\r\n"; } Files.write(Paths.get("./out.csv"), lines.getBytes()); System.out.println("INFO: Wrote results to 'out.csv'."); } catch (Throwable e) { System.out.println("ERROR: Could not write CSV File 'out.csv'. Error: " + e); } }
public ListItemReader<JsonObject> setResource() throws MalformedURLException { // Date date = new Date(); // String today= new SimpleDateFormat("yyyyMMdd").format(date); // local time이 일치하지 않으므로, 전날 종료된 경기정보 업데이트이므로 date -1 Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1); String today = new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); URL url = new URL("http://data.nba.com/data//json/nbacom/2015/gameline/" + today + "/games.json"); try (InputStream is = url.openStream(); JsonReader rdr = Json.createReader(is)) { JsonObject obj = rdr.readObject(); if (obj != null && obj.get("games") != null && obj.getJsonArray("games").size() > 0) { for (int idx = 0; idx < obj.getJsonArray("games").size(); idx++) { items.add(obj.getJsonArray("games").getJsonObject(idx)); } } } catch (IOException e) { e.printStackTrace(); } ListItemReader<JsonObject> reader = new ListItemReader<>(this.items); return reader; }
public static void main(String[] args) throws IOException { String workingDir = System.getProperty("user.dir"); Path filePath = FileSystems.getDefault().getPath(workingDir + "/resources/Day12"); JsonReader reader = Json.createReader(Files.newInputStream(filePath)); JsonStructure jsonst = reader.read(); System.out.printf("Value is %d\n", navigateTree(jsonst, null)); System.out.printf("Value is %d\n", navigateTree(jsonst, "red")); }
@Override public JsonObject getJsonObject() { try { try (JsonReader reader = Json.createReader(getResourceInputStream())) { return reader.readObject(); } } catch (Exception e) { throw new ResourceException("Error while getting the Json object", e); } }
/** * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet) */ @Override public void processPacket(Packet packet) { Message incomingMessage = (Message) packet; GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(GcmPacketExtension.GCM_NAMESPACE); String json = gcmPacket.getJson(); JsonReader jsonReader = Json.createReader(new StringReader(json)); JsonObject jsonObject = jsonReader.readObject(); Object messageType = jsonObject.get("message_type"); System.out.println(messageType); }
@Test public void statusFor() { try (JsonReader reader = Json.createReader(getClass().getResourceAsStream("site2CantTest.json"))) { Mesh mesh = Mesh.from(reader.readObject()); assertThat(mesh.statusFor(2, 0, Mesh.CellHalf.INITIATED_BY_ROW), equalTo(3)); assertThat(mesh.statusFor(2, 0, Mesh.CellHalf.INITIATED_BY_COLUMN), equalTo(0)); assertThat(mesh.statusFor(0, 2, Mesh.CellHalf.INITIATED_BY_ROW), equalTo(0)); assertThat(mesh.statusFor(0, 2, Mesh.CellHalf.INITIATED_BY_COLUMN), equalTo(3)); } }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); HttpSession user = req.getSession(); PrintWriter out = resp.getWriter(); SpejdResultAnalyser result = null; JsonObject response; JsonReader jsonReader = Json.createReader(new InputStreamReader(req.getInputStream(), StandardCharsets.ISO_8859_1)); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); System.out.println("REQUEST"); System.out.println(jsonObject); String originalText = jsonObject.getString("text"); int space = jsonObject.getInt("space"); String xml = getXml(originalText, user.getId()); try { result = new SpejdResultAnalyser(xml.replace("SYSTEM \"xcesAnaIPI.dtd\"", "")); } catch (ParserConfigurationException | SAXException e) { out.println(e.toString()); } assert result != null; /** do analysis of xml spejd result */ SubstantivePairs pairs = result.doAnalysis().makePair(space); /** * create json response { text: ... xml: ... pairs: [ { subst1: ... subst2: ... quantity: ... }, * ...] } */ JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); for (SubstantivePair pair : pairs) { arrayBuilder.add( Json.createObjectBuilder() .add("subst1", pair.getSubst1()) .add("subst2", pair.getSubst2()) .add("quantity", pair.getQuantity())); } Charset.forName("UTF-8").encode(originalText); JsonArray array = arrayBuilder.build(); response = Json.createObjectBuilder() .add("text", originalText) .add("xml", xml) .add("pairs", array) .build(); System.out.println("RESPONSE"); System.out.println(response); out.print(response); }
@Override public JsonResource setContents(InputStream data) { if (data == null) throw new IllegalArgumentException("InputStream must not be null."); try { try (JsonReader reader = Json.createReader(data)) { setContents(reader.read()); } } catch (Exception e) { throw new ResourceException("Error while setting the Json contents", e); } return this; }
/** * Parse and return the JsonObject for the payload * * @param a JsonObject for the parsed payload */ public JsonObject getParsedBody() { // We do not worry about concurrency here: processing any given message // is single threaded until queued (at which point we aren't looking // anymore) // We also are not using the more advanced streaming APIs, as the // messages the player service unpacks tend to be short and focused. if (jsonData == null) { JsonReader jsonReader = Json.createReader(new StringReader(messageData)); jsonData = jsonReader.readObject(); } return jsonData; }
public static boolean verify(String gRecaptchaResponse) throws IOException { if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) { return false; } try { URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); // add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String postParams = "secret=" + secret + "&response=" + gRecaptchaResponse; // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(postParams); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + url); System.out.println("Post parameters : " + postParams); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print result System.out.println(response.toString()); // parse JSON response and return 'success' value JsonReader jsonReader = Json.createReader(new StringReader(response.toString())); JsonObject jsonObject = jsonReader.readObject(); jsonReader.close(); return jsonObject.getBoolean("success"); } catch (Exception e) { return false; } }
// TODO: unicity of field name, better nested array/object handling private void generate( final JsonReaderFactory readerFactory, final File source, final Writer writer, final String javaName) throws MojoExecutionException { JsonReader reader = null; try { reader = readerFactory.createReader(new FileReader(source)); final JsonStructure structure = reader.read(); if (JsonArray.class.isInstance(structure) || !JsonObject.class.isInstance( structure)) { // quite redundant for now but to avoid surprises in future throw new MojoExecutionException( "This plugin doesn't support array generation, generate the model (generic) and handle arrays in your code"); } final JsonObject object = JsonObject.class.cast(structure); final Collection<String> imports = new TreeSet<String>(); // while we browse the example tree just store imports as well, avoids a 2 passes processing // duplicating imports logic final StringWriter memBuffer = new StringWriter(); generateFieldsAndMethods(memBuffer, object, " ", imports); if (header != null) { writer.write(header); writer.write('\n'); } writer.write("package " + packageBase + ";\n\n"); if (!imports.isEmpty()) { for (final String imp : imports) { writer.write("import " + imp + ";\n"); } writer.write('\n'); } writer.write("public class " + javaName + " {\n"); writer.write(memBuffer.toString()); writer.write("}\n"); } catch (final IOException e) { throw new MojoExecutionException(e.getMessage(), e); } finally { if (reader != null) { reader.close(); } } }
/** * This methods print outs public posts informations of given user sets the plusPosts with all * those information. getPlusPosts() can be used to get the information. * * @param userName : user name for a Google plus user to crawl the posts * @return no return value. */ public void fetchUserPosts(String userName) { try { InputStream is = new URL( "https://www.googleapis.com/plus/v1/people/" + userName + "/activities/public?key=" + API_KEY) .openStream(); JsonReader rdr = Json.createReader(is); JsonObject obj = rdr.readObject(); UserPost userPost = new UserPost(); userPost.setUserId(userName); JsonArray results = obj.getJsonArray("items"); if (results != null) { System.out.println("===================================="); System.out.println(" Posts"); System.out.println("===================================="); for (JsonObject result : results.getValuesAs(JsonObject.class)) { String postTitle = result.getString("title"); String postID = result.getString("id"); String postDate = result.getString("published"); System.out.println("\nPost ID: " + postID); System.out.println("Post Title: " + postTitle); System.out.println("Post Date: " + postDate); userPost.setPostId(postID); userPost.setPostText(postTitle); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); Date parsedDate = formatter.parse(postDate); userPost.setPostDate(parsedDate); userPost.setPlaceOfPost(""); getPlusPosts().add(userPost); System.out.println("___________________________________"); } } } catch (java.text.ParseException ex) { System.out.println(ex); } catch (IOException ex) { System.out.println(ex); } }
public SubDocument from(String subdocAsJson, SubDocType subDocType) { JsonReader reader = Json.createReader(new StringReader(subdocAsJson)); JsonObject jsonObject = reader.readObject(); SubDocument.Builder builder = new SubDocument.Builder(); for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) { Object objectValue = jsonValueToObject(entry.getValue()); convertAttribute(builder, entry.getKey(), objectValue, subDocType); } return builder.build(); }
private FlickrPhoto buildInfo(JsonReader jsonReader) { JsonObject jobj = jsonReader.readObject(); JsonObject object = (JsonObject) jobj.get("photo"); String id = object.getString("id"); FlickrPhoto p = photos.getPhoto(id); if (p == null) { p = new FlickrPhoto(); p.id = id; photos.setPhoto(p); } p.id = id; // JsonArray array = (JsonArray) object.get("dates"); object = (JsonObject) object.get("dates"); logger.debug("date=" + object.getString("taken")); logger.debug( "dateTaken=" + FlickrPhotoDate.setDateInFlickrTextFormat(object.getString("taken"))); p.dateTaken = FlickrPhotoDate.setDateInFlickrTextFormat(object.getString("taken")); logger.debug("DateTaken=" + p.dateTaken); return p; }
/** * this method should be called after clicktracking test, in order to verify if expected events * were tracked * * @author Michal 'justnpT' Nowierski */ public void compareTrackedEventsTo(List<JsonObject> expectedEventsList) { executeScript(ClickTrackingScriptsProvider.EVENTS_CAPTURE_INSTALLATION); List<JsonObject> trackedEventsArrayList = new ArrayList<JsonObject>(); List<JsonObject> trackedEventsList; JavascriptExecutor js = (JavascriptExecutor) driver; // prepare list of tracked events Object event = js.executeScript("return selenium_popEvent()"); StringReader reader = new StringReader(event.toString()); JsonReader jsonReader = Json.createReader(reader); while (!(event == null)) { reader = new StringReader(event.toString()); jsonReader = Json.createReader(reader); trackedEventsArrayList.add(jsonReader.readObject()); // take next tracked event event = js.executeScript("return selenium_popEvent()"); } trackedEventsList = trackedEventsArrayList; ClickTrackingSupport support = new ClickTrackingSupport(); support.compare(expectedEventsList, trackedEventsList); }
// Update the existing album with `id`. // // Display the modified album. private void actionPutAlbum(Router router, Request request, String id) { JsonReader reader = Json.createReader(new StringReader(request.body)); JsonObject album = reader.readObject(); for (JsonObject oalbum : catalog.getValuesAs(JsonObject.class)) { if (oalbum.getString("id").equals(id)) { JsonObjectBuilder builder = Json.createObjectBuilder(); for (String key : oalbum.keySet()) { builder.add(key, oalbum.get(key)); } for (String key : album.keySet()) { builder.add(key, album.get(key)); } // TODO add model layer and data management router.sendJsonResponse(200, "OK", builder.build()); return; } } // No album with this ID found, return error router.sendJsonError(404, "Not found"); }
public static void main(String[] args) { System.out.println("Starting client...."); String url = "http://127.0.0.1:8080/common/api/customers"; URI uri = URI.create(url); final Client client = ClientBuilder.newClient(); WebTarget webTarget = client.target(uri); Response response = webTarget.request(MediaType.APPLICATION_JSON).get(); // Se Response.Status.OK; if (response.getStatus() == 200) { StringReader stringReader = new StringReader(webTarget.request(MediaType.APPLICATION_JSON).get(String.class)); try (JsonReader jsonReader = Json.createReader(stringReader)) { // return jsonReader.readObject(); } } }
ObservableList<Question> getObservableList() throws IOException { String url = "http://api.stackexchange.com/2.2/search?tagged=javafx&site=stackoverflow"; URL host = new URL(url); JsonReader jr = Json.createReader(new GZIPInputStream(host.openConnection().getInputStream())); JsonObject jsonObject = jr.readObject(); JsonArray jsonArray = jsonObject.getJsonArray("items"); ObservableList<Question> answer = FXCollections.observableArrayList(); jsonArray .iterator() .forEachRemaining( (JsonValue e) -> { JsonObject obj = (JsonObject) e; JsonString name = obj.getJsonObject("owner").getJsonString("display_name"); JsonString quest = obj.getJsonString("title"); JsonNumber jsonNumber = obj.getJsonNumber("creation_date"); Question q = new Question(name.getString(), quest.getString(), jsonNumber.longValue() * 1000); answer.add(q); }); return answer; }
public static void parseVcap() throws Exception { if (URL != null && !URL.equals("")) { // If URL is already set, use it as is return; } // Otherwise parse URL and credentials from VCAP_SERVICES String serviceName = System.getenv("SERVICE_NAME"); if (serviceName == null || serviceName.length() == 0) { serviceName = SERVICE_NAME; } String vcapServices = System.getenv("VCAP_SERVICES"); if (vcapServices == null) { throw new Exception("VCAP_SERVICES not found in the environment"); } StringReader stringReader = new StringReader(vcapServices); JsonReader jsonReader = Json.createReader(stringReader); JsonObject vcap = jsonReader.readObject(); System.out.println("vcap: " + vcap); if (vcap.getJsonArray(serviceName) == null) { throw new Exception("Service " + serviceName + " not found in VCAP_SERVICES"); } if (USE_SSL) { URL = vcap.getJsonArray(serviceName) .getJsonObject(0) .getJsonObject("credentials") .getString("java_drda_url_ssl"); } else { URL = vcap.getJsonArray(serviceName) .getJsonObject(0) .getJsonObject("credentials") .getString("java_drda_url"); } System.out.println(URL); }
@Test public void getSites() { try (JsonReader reader = Json.createReader(getClass().getResourceAsStream("allWell.json"))) { Mesh mesh = Mesh.from(reader.readObject()); assertThat( mesh.getSites(), equalTo( Arrays.asList( "CERN-PROD LAT", "FZK-LCG2 LAT", "IN2P3-CC LAT", "INFN-T1 LAT", "KR-KISTI-GSDC-01 LAT", "NDGF-T1 LAT", "RAL-LCG2 LAT", "RRC-KI-T1 LAT", "SARA-MATRIX LAT", "TRIUMF-LCG2 LAT", "Taiwan-LCG2 LAT", "US-FNAL LT", "lhcperfmon-bnl", "pic LAT"))); } }
// 对接收的(click,mouseover,scroll)数据进行处理,并存入events @Path("/events/store") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) public void StoreEvents(String data, @Context HttpServletRequest request) throws UnsupportedEncodingException, IOException { String schema = "browser,os,url,ip,loadtime,time,element,id,text,semantics,event,left,top,height,width"; HashSet<String> hs_log = new HashSet<String>(); // System.out.println(data); // 读取获得的json数据 JsonReader reader = Json.createReader(new StringReader(data)); JsonObject jsonobj = reader.readObject(); reader.close(); // 获取来源IP,读取url,time,events等信息 String ip = ""; String browser = ""; String os = ""; String url = ""; String loadtime = ""; ip = getRemoteHost(request); browser = jsonobj.getString("browser"); os = jsonobj.getString("os"); url = jsonobj.getString("url").split("\\?")[0]; url = url.replace("http://", "").replace("HTTP://", "").replace("https://", ""); loadtime = jsonobj.getString("loadtime"); JsonArray events = jsonobj.getJsonArray("events"); // 整理数据 for (int i = 0; i < events.size(); i++) { JsonObject obj = events.getJsonObject(i); StringBuffer elebuf = new StringBuffer(); // 先存储头信息 elebuf.append(browser); elebuf.append(","); elebuf.append(os); elebuf.append(","); elebuf.append(url); elebuf.append(","); elebuf.append(ip); elebuf.append(","); elebuf.append(loadtime); elebuf.append(","); // 各个事件的信息 elebuf.append(obj.getString("time")); elebuf.append(","); elebuf.append(obj.getString("element")); elebuf.append(","); try { elebuf.append(obj.getString("id")); } catch (Exception e) { elebuf.append(""); } elebuf.append(","); elebuf.append(obj.getString("text")); elebuf.append(","); // 在这里可以引入SVM标注,标注后在存入数据库 // 或者可以读取数据在标注 elebuf.append(""); // semantics,标注后更新 elebuf.append(","); elebuf.append(obj.getString("event")); elebuf.append(","); elebuf.append(obj.getString("left")); elebuf.append(","); elebuf.append(obj.getString("top")); elebuf.append(","); elebuf.append(obj.getString("height")); elebuf.append(","); elebuf.append(obj.getString("width")); // 添加set中,去重 hs_log.add(elebuf.toString()); } eventsDAO.insertEvents(schema, hs_log); // return "Post Data Success!"; }
/** * This methods print outs public profile informations of given user sets the plusProfile with all * those information. getPlusProfile() can be used to get the information. * * @param userName : user name of a Google plus user to crawl the public profile informations. * @return no return value */ public void startCrawling(String userName) { try { InputStream is = new URL("https://www.googleapis.com/plus/v1/people/" + userName + "?key=" + API_KEY) .openStream(); JsonReader rdr = Json.createReader(is); JsonObject obj = rdr.readObject(); // retrieve user's profile information System.out.println("\nGoogle+ Profile crawler started...\n"); System.out.println("____________________________________"); System.out.println(" Profile"); System.out.println("===================================="); String displayName = obj.getJsonString("displayName").getString(); System.out.println("Name: " + displayName); getPlusProfile().setName(displayName); String user_id = obj.getJsonString("id").getString(); System.out.println("ID: " + user_id); // getPlusProfile().setUserId(user_id);//number format user id getPlusProfile().setUserId(userName); if (obj.containsKey("gender")) { String gender = obj.getJsonString("gender").getString(); System.out.println("Gender: " + gender); getPlusProfile().setGender(gender); } else { System.out.println("Gender: " + ""); getPlusProfile().setGender(""); } getPlusProfile().setDateOfBirth(""); JsonString occupationJson = obj.getJsonString("occupation"); if (occupationJson != null) { String occupation = obj.getJsonString("occupation").getString(); System.out.println("Occupation: " + occupation); } // retrieve user's work and education information JsonArray results = obj.getJsonArray("placesLived"); if (results != null) { System.out.println("____________________________________"); System.out.println(" Locations Lived"); System.out.println("===================================="); int i = 0; for (JsonObject result : results.getValuesAs(JsonObject.class)) { String location = result.getString("value"); System.out.println(location); if (i == 0) getPlusProfile().setCurrentLocation(location); else getPlusProfile().setHomeLocation(location); i++; } if (i == 1) getPlusProfile().setHomeLocation(getPlusProfile().getCurrentLocation()); } else { System.out.println("Location info NOT AVAILABLE"); getPlusProfile().setCurrentLocation(""); getPlusProfile().setHomeLocation(""); } // retrieve user's work and education information Set<String> wrkEdu = new TreeSet<String>(); results = obj.getJsonArray("organizations"); if (results != null) { System.out.println("____________________________________"); System.out.println(" Work and Education"); System.out.println("===================================="); for (JsonObject result : results.getValuesAs(JsonObject.class)) { if (result.containsKey("name")) { String workEdu = result.getString("name"); System.out.println(workEdu); } else { wrkEdu.add(""); System.out.println("Company name NOT AVAILABLE"); } } getPlusProfile().setEmployer(wrkEdu); getPlusProfile().setEducation(wrkEdu); } else { System.out.println("Work and Education info NOT AVAILABLE"); getPlusProfile().setEmployer(wrkEdu); getPlusProfile().setEducation(wrkEdu); } List<String> languages = new ArrayList<String>(); getPlusProfile().setLanguages(languages); firefoxDriver = new FirefoxDriver(); firefoxDriver.get("https://plus.google.com/" + userName + "/about"); Thread.sleep(3000); // get list of circles List<WebElement> circle = firefoxDriver.findElements(By.className("bkb")); Set<String> circles = new TreeSet<String>(); if (circle.size() != 0) { String circleText = circle.get(0).getText(); System.out.println(circleText); String circleCountstr = circleText.split("\\s+")[0]; if (circleCountstr.contains(",")) { circleCountstr = circleCountstr.replace(",", ""); } int circleCount = Integer.parseInt(circleCountstr); System.out.println("Circle Count:" + circleCount); WebElement circleLink = firefoxDriver.findElement(By.xpath("//span[contains(text(),'" + circleText + "')]")); circleLink.click(); Thread.sleep(3000); WebElement friendListBox = firefoxDriver.findElement(By.className("G-q-B")); Robot robot = new Robot(); int loop = 0; while (loop < circleCount / 10) { if (circles.size() == circleCount) break; Thread.sleep(3000); List<WebElement> friendList = friendListBox.findElements(By.tagName("a")); for (WebElement friend : friendList) { System.out.println(friend.getText()); circles.add(friend.getText()); } int count = 0; while (count < 500) { robot.keyPress(java.awt.event.KeyEvent.VK_DOWN); count++; } loop++; } } List<String> friends = new ArrayList<String>(); for (String friend : circles) { friends.add(friend); } getPlusProfile().setFriends(friends); System.out.println( "_________________________________________________________________________________________"); System.out.println("Finish Crawling..."); closePlusWebDriverNode(); } catch (AWTException ex) { System.out.println("Problem in key press:" + ex); } catch (IOException ex) { System.out.println(ex); } catch (InterruptedException ex) { System.out.println(); } }
private boolean isJsonResult(String response) { JsonReader jsonReader = Json.createReader(new StringReader(response)); JsonObject json = jsonReader.readObject(); return json.getString("realname").equals("Richard David James"); }