List of usage examples for org.json.simple JSONObject keySet
Set<K> keySet();
From source file:com.versul.main.MainClass.java
private ComponentBuilder<?, ?> createSumComponent(String label) throws IOException { if (aggregations != null && aggregations.containsKey("sum")) { JSONArray sumArray = (JSONArray) aggregations.get("sum"); if (sumArray != null && !sumArray.isEmpty()) { String key, value;/*ww w.j a v a 2 s . co m*/ VerticalListBuilder listSuper = cmp.verticalList(); for (Object sumObject : sumArray) { HorizontalListBuilder cardComponent = createCardComponent(); JSONObject sumField = (JSONObject) sumObject; key = (String) sumField.keySet().toArray()[0]; log("key " + key); value = String.valueOf(sumField.get(key)); log("value " + sumField.get(key)); VerticalListBuilder content = cmp.verticalList(); HorizontalListBuilder labelSum = cmp.horizontalList(); labelSum.add(cmp.text("<b>" + key + "</b> : " + value).setStyle(getStyleMarkedUp())); content.add(labelSum); // content.setFixedWidth(150); cardComponent.add(content); listSuper.add(cardComponent); } return cmp.verticalList(cmp.text(label).setStyle(templateDefault.boldStyle), listSuper); } } return null; }
From source file:couchdb.CouchdbClient.java
@Override public int scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) { ViewResult viewResult = this.executeView(startkey, recordcount); for (Row row : viewResult.getRows()) { JSONObject jsonObj = this.parseAsJsonObject(row.getDoc()); if (jsonObj == null) return JSON_PARSING_FAULT; if (fields == null) { @SuppressWarnings("unchecked") Set<String> requestedFields = jsonObj.keySet(); result.add(this.getFieldsFromJsonObj(requestedFields, jsonObj)); } else {/*from w w w.j a v a 2 s .c o m*/ result.add(this.getFieldsFromJsonObj(fields, jsonObj)); } } return OK; }
From source file:com.ge.research.semtk.load.DataCleaner.java
/** * Parse a JSON object containing the cleaning specs *//* w w w . j av a 2s.c om*/ private void parseCleanSpecJson(JSONObject cleanSpecJson) throws Exception { if (cleanSpecJson == null) { return; } // add lower case specs JSONArray lowercaseObj = (JSONArray) cleanSpecJson.get(JSON_KEY_LOWERCASE); if (lowercaseObj != null) { for (Object jObj : (JSONArray) cleanSpecJson.get(JSON_KEY_LOWERCASE)) { addToLowerCase((String) jObj); } } // add split specs JSONObject splitObj = (JSONObject) cleanSpecJson.get(JSON_KEY_SPLIT); if (splitObj != null) { for (String s : (Set<String>) splitObj.keySet()) { addSplit(s, (String) splitObj.get(s)); } } // add remove nulls specs String removeNullsVal = (String) cleanSpecJson.get(JSON_KEY_REMOVE_NULLS); if (removeNullsVal != null) { if (removeNullsVal.toString().toLowerCase().equals("true")) { removeNulls = true; } } // add remove N/A specs String removeNAVal = (String) cleanSpecJson.get(JSON_KEY_REMOVE_NA); if (removeNAVal != null) { if (removeNAVal.toString().toLowerCase().equals("true")) { removeNA = true; } } }
From source file:com.versul.main.MainClass.java
private ComponentBuilder<?, ?> createGroupsComponent(String label) throws IOException { if (aggregations != null && aggregations.containsKey("group")) { JSONArray groups = (JSONArray) aggregations.get("group"); if (groups != null && !groups.isEmpty()) { VerticalListBuilder listSuper = cmp.verticalList(); String key;/*from w w w.j a va 2s . com*/ JSONArray group; for (Object groupObj : groups) { HorizontalListBuilder cardComponent = createCardComponent(); // Aggregation object JSONObject groupField = (JSONObject) groupObj; key = (String) groupField.keySet().toArray()[0]; group = (JSONArray) groupField.get(key); VerticalListBuilder content = cmp.verticalList(); content.add(cmp.text(key)); for (Object segmentObj : group) { String groupText = ""; JSONObject segment = (JSONObject) segmentObj; Object[] segmentKeys = segment.keySet().toArray(); for (Object statisticKey : segmentKeys) { groupText += " " + statisticKey.toString() + ": "; JSONObject statistics = (JSONObject) segment.get(statisticKey); Object[] statisticKeys = statistics.keySet().toArray(); String totalKey = statisticKeys[0].toString(); String totalObject = statistics.get(totalKey).toString(); groupText += "<b>(" + totalKey + "</b> : " + totalObject; if (statisticKeys.length > 1) { groupText += " | "; String amountKey = statisticKeys[1].toString(); String amountObject = statistics.get(amountKey).toString(); groupText += "<b>" + amountKey + "</b> : " + amountObject; } groupText += ")"; content.add(cmp.text(groupText).setStyle(getStyleMarkedUp())); } } cardComponent.add(content); listSuper.add(cardComponent); } return cmp.verticalList(cmp.text(label).setStyle(templateDefault.boldStyle), listSuper); } } return null; }
From source file:com.opensoc.parsing.parsers.BasicBroParser.java
@SuppressWarnings("unchecked") public JSONObject parse(byte[] msg) { _LOG.trace("[OpenSOC] Starting to parse incoming message"); String raw_message = null;/*from ww w . jav a 2 s .c o m*/ try { raw_message = new String(msg, "UTF-8"); _LOG.trace("[OpenSOC] Received message: " + raw_message); JSONObject cleaned_message = cleaner.Clean(raw_message); _LOG.debug("[OpenSOC] Cleaned message: " + raw_message); if (cleaned_message == null || cleaned_message.isEmpty()) throw new Exception("Unable to clean message: " + raw_message); String key = cleaned_message.keySet().iterator().next().toString(); if (key == null) throw new Exception("Unable to retrieve key for message: " + raw_message); JSONObject payload = (JSONObject) cleaned_message.get(key); String originalString = " |"; for (Object k : payload.keySet()) { originalString = originalString + " " + k.toString() + ":" + payload.get(k).toString(); } originalString = key.toUpperCase() + originalString; payload.put("original_string", originalString); if (payload == null) throw new Exception("Unable to retrieve payload for message: " + raw_message); if (payload.containsKey("ts")) { String ts = payload.remove("ts").toString(); payload.put("timestamp", ts); _LOG.trace("[OpenSOC] Added ts to: " + payload); } if (payload.containsKey("id.orig_h")) { String source_ip = payload.remove("id.orig_h").toString(); payload.put("ip_src_addr", source_ip); _LOG.trace("[OpenSOC] Added ip_src_addr to: " + payload); } else if (payload.containsKey("tx_hosts")) { JSONArray txHosts = (JSONArray) payload.remove("tx_hosts"); if (txHosts != null && !txHosts.isEmpty()) { payload.put("ip_src_addr", txHosts.get(0)); _LOG.trace("[OpenSOC] Added ip_src_addr to: " + payload); } } if (payload.containsKey("id.resp_h")) { String source_ip = payload.remove("id.resp_h").toString(); payload.put("ip_dst_addr", source_ip); _LOG.trace("[OpenSOC] Added ip_dst_addr to: " + payload); } else if (payload.containsKey("rx_hosts")) { JSONArray rxHosts = (JSONArray) payload.remove("rx_hosts"); if (rxHosts != null && !rxHosts.isEmpty()) { payload.put("ip_dst_addr", rxHosts.get(0)); _LOG.trace("[OpenSOC] Added ip_dst_addr to: " + payload); } } if (payload.containsKey("id.orig_p")) { String source_port = payload.remove("id.orig_p").toString(); payload.put("ip_src_port", source_port); _LOG.trace("[OpenSOC] Added ip_src_port to: " + payload); } if (payload.containsKey("id.resp_p")) { String dest_port = payload.remove("id.resp_p").toString(); payload.put("ip_dst_port", dest_port); _LOG.trace("[OpenSOC] Added ip_dst_port to: " + payload); } // if (payload.containsKey("host")) { // // String host = payload.get("host").toString().trim(); // String tld = tldex.extractTLD(host); // // payload.put("tld", tld); // _LOG.trace("[OpenSOC] Added tld to: " + payload); // // } // if (payload.containsKey("query")) { // String host = payload.get("query").toString(); // String[] parts = host.split("\\."); // int length = parts.length; // if (length >= 2) { // payload.put("tld", parts[length - 2] + "." // + parts[length - 1]); // _LOG.trace("[OpenSOC] Added tld to: " + payload); // } // } _LOG.trace("[OpenSOC] Inner message: " + payload); payload.put("protocol", key); _LOG.debug("[OpenSOC] Returning parsed message: " + payload); return payload; } catch (Exception e) { _LOG.error("Unable to Parse Message: " + raw_message); e.printStackTrace(); return null; } }
From source file:org.opencastproject.adminui.endpoint.UsersSettingsEndpointTest.java
private void compareIds(String key, JSONObject expected, JSONObject actual) { JSONArray expectedArray = (JSONArray) expected.get(key); JSONArray actualArray = (JSONArray) actual.get(key); Assert.assertEquals(expectedArray.size(), actualArray.size()); JSONObject exObject; JSONObject acObject;//from w w w . ja va2 s. co m int actualId; for (int i = 0; i < actualArray.size(); i++) { acObject = (JSONObject) actualArray.get(i); actualId = Integer.parseInt(acObject.get("id").toString()) - 1; exObject = (JSONObject) expectedArray.get(actualId); Set<String> exEntrySet = exObject.keySet(); Assert.assertEquals(exEntrySet.size(), acObject.size()); Iterator<String> exIter = exEntrySet.iterator(); while (exIter.hasNext()) { String item = exIter.next(); Object exValue = exObject.get(item); Object acValue = acObject.get(item); Assert.assertEquals(exValue, acValue); } } }
From source file:de.jaide.courier.email.MessageHandlerEMail.java
/** * Loads the SMTP configuration from a JSON file. * /* w w w . j a v a 2 s . com*/ * @param smtpConfigurationJsonLocation The SMTP configuration to load. Needs to be an absolute URL, e.g. "/configs/smtp.json" that is * loaded from the * classpath. * @throws IOException Thrown, if the SMTP configuration couldn't be read. */ private void loadSmtpConfigurations(String smtpConfigurationJsonLocation) { /* * Now load the SMTP configuration JSON file and try to parse it. */ try { /* * Load the JSON-based SMTP configuration file. */ JSONArray keyArray = (JSONArray) new JSONParser() .parse(IOUtils.toString((InputStream) MessageHandlerEMail.class .getResource(smtpConfigurationJsonLocation).getContent())); /* * Iterate through the outer array */ for (int i = 0; i < keyArray.size(); i++) { /* * Iterate through the inner array of configuration keys. */ JSONObject keysArray = (JSONObject) keyArray.get(i); for (Object keyObject : keysArray.keySet()) { /* * Get the name of the key... */ String key = (String) keyObject; JSONObject configArray = (JSONObject) keysArray.get(key); /* * ... and load the associated configuration values. */ String smtpHostname = (String) configArray.get("smtpHostname"); Long smtpPort = (Long) (configArray.get("smtpPort")); Boolean tls = (Boolean) configArray.get("tls"); Boolean ssl = (Boolean) configArray.get("ssl"); String username = (String) configArray.get("username"); String password = (String) configArray.get("password"); String fromEMail = (String) configArray.get("fromEMail"); String fromSenderName = (String) configArray.get("fromSenderName"); /* * Use the obtained values and create a new SMTP configuration. */ SmtpConfiguration smtpConfiguration = new SmtpConfiguration(key, smtpHostname, smtpPort.intValue(), tls, ssl, username, password, fromEMail, fromSenderName); smtpConfigurations.put(key, smtpConfiguration); } } } catch (IOException ioe) { throw new RuntimeException("SMTP configuration not found at '" + smtpConfigurationJsonLocation + "'", ioe); } catch (ParseException pe) { throw new RuntimeException( "SMTP configuration couldn't be loaded from '" + smtpConfigurationJsonLocation + "'", pe); } }
From source file:de.instantouch.model.io.SnakeJSONReader.java
public void readEntity(SnakeEntity entity, Object value) throws SnakeModelException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { if (!(value instanceof JSONObject)) { throw new SnakeWrongTypeException( "wrong type for entity: " + entity.getName() + " candidate: " + value); }/*from w ww .ja va 2s .c o m*/ JSONObject jsonObject = (JSONObject) value; for (SnakeType member : entity.getChildren()) { Object object = jsonObject.get(member.getName()); if (object != null) { read(member, object); } } // dynamic members for (Object key : jsonObject.keySet()) { String name = key.toString(); SnakeType member = entity.findByName(name); if (member == null) { Object obj = jsonObject.get(key); if (obj instanceof JSONObject) { JSONObject inner = (JSONObject) obj; Object classInfo = inner.get("entityClass"); Object bundleInfo = inner.get("bundle"); Bundle bundle = Platform.getBundle(bundleInfo.toString()); SnakeEntity child = (SnakeEntity) bundle.loadClass(classInfo.toString()).newInstance(); child.setName(name); readEntity(child, inner); entity.replace(name, child); } } } }
From source file:cc.siara.csv_ml.ParsedObject.java
/** * Deletes all attributes with name "parent_ref" in all levels in a * JSONObject hierarchy recursively.// w ww .j a va 2 s . c om * * @param obj * Initially pass the root object */ private void deleteParentRefsRecursively(JSONObject obj) { if (obj.containsKey("parent_ref")) obj.remove("parent_ref"); // Go through each child object or array // recursively for (Object set : obj.keySet()) { String key = (String) set; Object child_obj = obj.get(key); if (child_obj instanceof JSONArray) { JSONArray ja = (JSONArray) child_obj; for (Object ele : ja) { if (ele instanceof JSONObject) deleteParentRefsRecursively((JSONObject) ele); } } else if (child_obj instanceof JSONObject) { deleteParentRefsRecursively((JSONObject) child_obj); } } }
From source file:com.capitalone.dashboard.util.ClientUtil.java
/** * Converts a Jira string representation of sprint artifacts into a POJO Map * object, with string as keys.//from w w w.j a v a 2 s . c o m * * @param nativeRs * a sanitized String representation of a sprint artifact link * from Jira * @return A canonical Map of Jira sprint artifacts */ public Map<String, Object> toCanonicalSprintPOJO(String nativeRs) { JSONObject nativeSprint = this.toCanonicalSprintJSON(nativeRs.substring(1, nativeRs.length() - 2)); Map<String, Object> canonicalSprint = new HashMap<String, Object>(); if ((nativeSprint != null) && !(nativeSprint.isEmpty())) { @SuppressWarnings("unchecked") Set<String> keys = nativeSprint.keySet(); Iterator<String> keysItr = keys.iterator(); while (keysItr.hasNext()) { String key = keysItr.next(); Object value = nativeSprint.get(key); if (value instanceof JSONArray) { try { value = this.toList((JSONArray) value); } catch (JSONException e) { value = new ArrayList<String>(); } } else if (value instanceof JSONObject) { value = this.toCanonicalSprintPOJO(value.toString()); } canonicalSprint.put(key, value); } } return canonicalSprint; }