List of usage examples for org.json.simple JSONArray get
public E get(int index)
From source file:de.mpg.imeji.presentation.servlet.autocompleter.java
/** * Parse a JSON file from CoNE with authors, and return a JSON which can be read by imeji autocomplete * /*from w ww . j ava 2 s . com*/ * @param cone * @return * @throws IOException */ @SuppressWarnings("unchecked") private String parseConeAuthor(String cone) throws IOException { Object obj = JSONValue.parse(cone); JSONArray array = (JSONArray) obj; JSONArray result = new JSONArray(); for (int i = 0; i < array.size(); ++i) { JSONObject parseObject = (JSONObject) array.get(i); JSONObject sendObject = new JSONObject(); sendObject.put("label", parseObject.get("http_purl_org_dc_elements_1_1_title")); sendObject.put("family", parseObject.get("http_xmlns_com_foaf_0_1_family_name")); sendObject.put("givenname", parseObject.get("http_xmlns_com_foaf_0_1_givenname")); sendObject.put("id", parseObject.get("id")); sendObject.put("orgs", writeJsonArrayToOneString(parseObject.get("http_purl_org_escidoc_metadata_terms_0_1_position"), "http_purl_org_eprint_terms_affiliatedInstitution")); sendObject.put("alternatives", writeJsonArrayToOneString(parseObject.get("http_purl_org_dc_terms_alternative"), "")); result.add(sendObject); } StringWriter out = new StringWriter(); JSONArray.writeJSONString(result, out); return out.toString(); }
From source file:me.timothy.ddd.entities.EntityManager.java
public void loadEntities(File file) throws IOException, ParseException { logger.printf(Level.INFO, "Loading entities from %s (exists: %b)", file.getCanonicalPath(), file.exists()); JSONArray jsonArray; try (FileReader fr = new FileReader(file)) { JSONParser parser = new JSONParser(); jsonArray = (JSONArray) parser.parse(fr); }//from w ww . j a v a2 s . com for (int i = 0; i < jsonArray.size(); i++) { EntityInfo ei = new EntityInfo(); ei.loadFrom((JSONObject) jsonArray.get(i)); logger.trace(ei.toString()); entities.add(new Entity(ei)); } logger.printf(Level.INFO, "Successfully loaded %d entities", entities.size()); }
From source file:com.zb.app.biz.service.WeixinTest.java
private boolean compareFakeid(String fakeid, String openid) { PostMethod post = new PostMethod( "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&token=" + token + "&tofakeid=" + fakeid + "&lang=zh_CN"); post.setRequestHeader("Cookie", this.cookiestr); post.setRequestHeader("Host", "mp.weixin.qq.com"); post.setRequestHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&token=" + token + "&lang=zh_CN&pagesize=10&pageidx=0&type=0"); post.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); post.addParameter(new NameValuePair("token", token)); post.addParameter(new NameValuePair("ajax", "1")); try {/*www. j a va 2 s . c o m*/ int code = httpClient.executeMethod(post); if (HttpStatus.SC_OK == code) { String str = post.getResponseBodyAsString(); String msgJson = StringUtils.substringBetween(str, "<script id=\"json-msgList\" type=\"json\">", "</script>"); JSONParser parser = new JSONParser(); try { JSONArray array = (JSONArray) parser.parse(msgJson); for (int i = 0; i < array.size(); i++) { JSONObject obj = (JSONObject) array.get(i); String content = (String) obj.get("content"); if (content.contains(openid)) { return true; } } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:com.zb.app.biz.service.WeixinTest.java
private boolean _compareFakeid(String fakeid, String openid) { String _url = "http://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&token=" + token + "&tofakeid=" + fakeid + "&lang=zh_CN"; GetMethod get = new GetMethod(_url); get.setRequestHeader("Cookie", this.cookiestr); get.setRequestHeader("Host", "mp.weixin.qq.com"); get.setRequestHeader("Referer", "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid=0&token=" + token + "&lang=zh_CN"); get.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); try {/*from w w w.j a v a 2 s . c o m*/ int code = httpClient.executeMethod(get); if (HttpStatus.SC_OK == code) { String str = get.getResponseBodyAsString(); String msgJson = StringUtils.substringBetween(str, "{\"msg_item\":", "}};"); JSONParser parser = new JSONParser(); try { JSONArray array = (JSONArray) parser.parse(msgJson); for (int i = 0; i < array.size(); i++) { JSONObject obj = (JSONObject) array.get(i); String content = (String) obj.get("content"); if (content.contains(openid)) { return true; } } } catch (Exception e) { // log.error(e.getMessage(), e); } } } catch (Exception e) { // log.error(e.getMessage(), e); } return false; }
From source file:de.instantouch.model.io.SnakeJSONReader.java
public void readList(SnakeList<SnakeType> myList, Object value) throws SnakeModelException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { if (!(value instanceof JSONArray)) { throw new SnakeWrongTypeException( "couldn't extract list: " + myList.getName() + " from json object:" + value); }/*from ww w. ja va2s.co m*/ JSONArray jsonArray = (JSONArray) value; myList.clear(); for (int i = 0; i < jsonArray.size(); i++) { Object object = jsonArray.get(i); SnakeType child = myList.newElement(); if (child == null) { SnakeLog.error( "couldn't read list data: " + object + "for: " + myList.getClass().getCanonicalName()); break; } child.setParent(myList); read(child, object); myList.add(child); } }
From source file:com.asus.ctc.eebot.ie.externalresources.conceptnet.JsonDecoder.java
/** * This code extracts the edges from json; * /*from w w w . j a v a2 s . c om*/ * @param edges * @return */ private ConceptNetDataStructure createConceptNetEdges(Object edges) { ConceptNetDataStructure cds = new ConceptNetDataStructure(); List<ConceptNetEdge> edgeList = new LinkedList<ConceptNetEdge>(); String conceptDescription = ""; JSONArray edgeJsonArray = (JSONArray) edges; for (int i = 0; i < edgeJsonArray.size(); i++) { JSONObject jobj = (JSONObject) edgeJsonArray.get(i); String start = (String) jobj.get("start"); String rel = (String) jobj.get("rel"); String end = (String) jobj.get("end"); if (start.contains("/c/en/")) { start = start.replace("/c/en/", ""); } if (end.contains("/c/en/")) { end = end.replace("/c/en/", ""); } if (rel.contains("/r/")) { rel = rel.replace("/r/", ""); } ConceptNetEdge edge = new ConceptNetEdge(); edge.setStart(start); edge.setRel(rel); edge.setEnd(end); createEdgeNLG(edge); edgeList.add(edge); conceptDescription += edge.getNlg() + ". "; // System.out.println(start + " " + rel + " "+ end); } cds.setEdges(edgeList); cds.setConceptDescription(conceptDescription); return cds; }
From source file:kr.co.bitnine.octopus.testutils.MemoryDatabaseTest.java
@Test public void testImport() throws Exception { MemoryDatabase memDb = new MemoryDatabase("sample"); memDb.start();/* w w w . j a v a 2s . co m*/ final String jsonFileName = "/sample.json"; memDb.importJSON(getClass(), jsonFileName); JSONParser jsonParser = new JSONParser(); JSONArray tables = (JSONArray) jsonParser .parse(new InputStreamReader(getClass().getResourceAsStream(jsonFileName))); assertEquals(2, tables.size()); Connection conn = memDb.getConnection(); Statement stmt = conn.createStatement(); JSONObject employee = (JSONObject) tables.get(0); ResultSet rs = stmt.executeQuery("SELECT * FROM \"employee\""); verifyTableEquals(employee, rs); rs.close(); JSONObject team = (JSONObject) tables.get(1); rs = stmt.executeQuery("SELECT * FROM \"team\""); verifyTableEquals(team, rs); rs.close(); stmt.close(); conn.close(); memDb.stop(); }
From source file:com.walmartlabs.mupd8.application.Config.java
@SuppressWarnings("unchecked") private void loadAppConfig(String filename) throws Exception { JSONObject appJson = (JSONObject) JSONValue.parse(readWithPreprocessing(new FileReader(filename))); JSONObject applicationNameObject = new JSONObject(); String applicationName = (String) appJson.get("application"); applicationNameObject.put(applicationName, appJson); JSONObject mupd8 = (JSONObject) configuration.get("mupd8"); mupd8.put("application", applicationNameObject); if (appJson.containsKey("performers")) { JSONArray performers = (JSONArray) appJson.get("performers"); for (int i = 0; i < performers.size(); i++) { JSONObject json = (JSONObject) performers.get(i); String performer = (String) json.get("performer"); workerJSONs.put(performer, json); }/*from www .j a v a 2 s. c om*/ } }
From source file:geo.controller.GeoCodeServlet.java
/** * * Invoked Java URL Connection to retrieve Token from AccessKey provided by * OneMap API./*ww w . j a v a 2s. c o m*/ * * A JSON object will be returned from the request call. We will Parse the * JSON using the simple JSON library and filter out the token required to * invoke OneMaps' geocoding service. * * @return token from OneMapAPI * @throws ServletException * @throws IOException */ private String getTokenFromAPI() throws ServletException, IOException { String token = null; URL oneMapToken = new URL("http://www.onemap.sg/API/services.svc/getToken?accessKEY=" + accessKey); URLConnection yc = oneMapToken.openConnection(); BufferedReader reader = new BufferedReader(new InputStreamReader(yc.getInputStream())); String json = reader.readLine(); JSONParser parser = new JSONParser(); Object obj; try { obj = parser.parse(json); JSONObject jsonObject = (JSONObject) obj; JSONArray jsonArray = (JSONArray) jsonObject.get("GetToken"); jsonObject = (JSONObject) jsonArray.get(0); token = jsonObject.get("NewToken").toString(); } catch (ParseException ex) { ex.printStackTrace(); } return token; }
From source file:de.mpg.imeji.presentation.servlet.autocompleter.java
/** * Parse a json input from Google Geo API * /*from www . j a v a 2 s .c om*/ * @param google * @return * @throws IOException */ @SuppressWarnings("unchecked") private String parseGoogleGeoAPI(String google) throws IOException { JSONObject obj = (JSONObject) JSONValue.parse(google); JSONArray array = (JSONArray) obj.get("results"); JSONArray result = new JSONArray(); for (int i = 0; i < array.size(); ++i) { JSONObject parseObject = (JSONObject) array.get(i); JSONObject sendObject = new JSONObject(); sendObject.put("label", parseObject.get("formatted_address")); sendObject.put("value", parseObject.get("formatted_address")); JSONObject location = (JSONObject) ((JSONObject) parseObject.get("geometry")).get("location"); sendObject.put("latitude", location.get("lat")); sendObject.put("longitude", location.get("lng")); result.add(sendObject); } StringWriter out = new StringWriter(); result.writeJSONString(out); return out.toString(); }