List of usage examples for org.json.simple JSONObject entrySet
Set<Map.Entry<K, V>> entrySet();
From source file:hoot.services.controllers.job.JobControllerBase.java
protected static JSONArray parseParams(String params) throws ParseException { JSONParser parser = new JSONParser(); JSONObject command = (JSONObject) parser.parse(params); JSONArray commandArgs = new JSONArray(); for (Object o : command.entrySet()) { Map.Entry<Object, Object> mEntry = (Map.Entry<Object, Object>) o; String key = (String) mEntry.getKey(); String val = (String) mEntry.getValue(); JSONObject arg = new JSONObject(); arg.put(key, val); commandArgs.add(arg);//from w ww . j a v a2 s . c om } return commandArgs; }
From source file:com.p000ison.dev.simpleclans2.util.JSONUtil.java
public static Map<Integer, Boolean> JSONToPermissionMap(String json, Map<Integer, Boolean> output) { if (json == null || json.isEmpty()) { return null; }/*from w w w. j av a2 s. co m*/ JSONObject parser = (JSONObject) JSONValue.parse(json); if (parser == null) { return null; } for (Object obj : parser.entrySet()) { Map.Entry entry = (Map.Entry) obj; Integer integer; // //Keys in json maps are always strings! // if (entry.getKey() instanceof String) { integer = Integer.parseInt((String) entry.getKey()); } else { integer = (Integer) entry.getKey(); } output.put(integer, (Boolean) entry.getValue()); } return output; }
From source file:com.conwet.silbops.model.AbstractMapping.java
/** * Convert attribute-value mapping from its JSON representation. * * @param clazz the Class of the new type to instantiate * @param json JSON representation of a mapping between attributes and values * @return the new instance created//from w ww . jav a 2 s . c o m * @throws IllegalArgumentException if some problem occur parsing JSON or * when creating instance class */ @SuppressWarnings("unchecked") protected static <U extends AbstractMapping<U>> U fromJSON(Class<U> clazz, JSONObject json) { try { U instance = clazz.newInstance(); for (Entry<String, Object> entry : (Set<Entry<String, Object>>) json.entrySet()) { instance.attribute(Attribute.fromJSON(entry.getKey()), Value.fromJSON(entry.getValue())); } return instance; } catch (InstantiationException | IllegalAccessException ex) { throw new IllegalArgumentException("Unable to load instance of class " + clazz, ex); } }
From source file:br.com.blackhubos.eventozero.updater.assets.uploader.Uploader.java
@SuppressWarnings("unchecked") public static Optional<Uploader> parseJsonObject(JSONObject parse, MultiTypeFormatter formatter) { long id = Long.MIN_VALUE; String name = null;/*www .ja v a2s . c o m*/ boolean admin = false; // Loop em todas entradas do JSON for (Map.Entry entries : (Set<Map.Entry>) parse.entrySet()) { Object key = entries.getKey(); Object value = entries.getValue(); String valueString = String.valueOf(value); /** Transforma o objeto em um {@link AssetUploaderInput) para usar com switch **/ switch (AssetUploaderInput.parseObject(key)) { case ADMIN: { // Obtem o valor que indica se quem enviou era administrador if (formatter.canFormat(Boolean.class)) { Optional<Boolean> result = formatter.format(value, Boolean.class); if (result.isPresent()) admin = result.get(); } break; } case ID: { // Obtm o ID do usurio id = Long.parseLong(valueString); break; } case LOGIN: { // Obtm o nome/login do usurio name = valueString; break; } default: { break; } } } if (id == Long.MIN_VALUE || name == null) { return Optional.empty(); } return Optional.of(new Uploader(name, admin, id)); }
From source file:com.conwet.silbops.model.ContextFunction.java
@SuppressWarnings("unchecked") public static ContextFunction fromJSON(JSONObject json) { ContextFunction func = new ContextFunction(); for (Entry<String, JSONArray> entry : (Set<Entry<String, JSONArray>>) json.entrySet()) { try {// w w w . j a v a 2 s . c o m JSONObject key = (JSONObject) new JSONParser().parse(entry.getKey()); Value value = Value.fromJSON(entry.getValue()); func.constraint.put(Key.fromJSON(key), value); } catch (ParseException ex) { throw new IllegalArgumentException("Unable to parse Key: " + entry.getKey(), ex); } } return func; }
From source file:agileinterop.AgileInterop.java
private static JSONObject createRelatedPSRs(String body) throws UnsupportedOperationException, ParseException, APIException { // Parse body as JSON JSONParser parser = new JSONParser(); JSONObject jsonBody = (JSONObject) parser.parse(body); Map<String, List<String>> data = new HashMap<>(); for (Iterator it = jsonBody.entrySet().iterator(); it.hasNext();) { Map.Entry pair = (Map.Entry) it.next(); String psrNumber = (String) pair.getKey(); JSONArray relatedPSRTypes = (JSONArray) pair.getValue(); List<String> relatedPSRNumbers = new ArrayList<>(); for (Object relatedPSRType : relatedPSRTypes) { String relatedPSRNumber = Agile.createRelatedPSR(psrNumber, (String) relatedPSRType); relatedPSRNumbers.add(relatedPSRNumber); }/* www. ja v a 2s . c o m*/ data.put(psrNumber, relatedPSRNumbers); } JSONObject obj = new JSONObject(); obj.put("data", data); return obj; }
From source file:com.worldline.easycukes.commons.helpers.JSONHelper.java
/** * Returns <b>true</b> if JSON object o1 is equals to JSON object o2. * * @param o1 a {@link JSONObject} containing some JSON data * @param o2 another {@link JSONObject} containing some JSON data * @return true if the json objects are equals *///from ww w .ja v a 2 s . c o m public static boolean equals(@NonNull JSONObject o1, @NonNull JSONObject o2) { if (o1 == o2) return true; if (o1.size() != o2.size()) return false; try { final Iterator<Entry<String, Object>> i = o1.entrySet().iterator(); while (i.hasNext()) { final Entry<String, Object> e = i.next(); final String key = e.getKey(); final Object value1 = e.getValue(); final Object value2 = o2.get(key); if (value1 == null) { if (!(o2.get(key) == null && o2.containsKey(key))) return false; } else if (value1 instanceof JSONObject) { if (!(value2 instanceof JSONObject)) return false; if (!equals((JSONObject) value1, (JSONObject) value2)) return false; } else if (value1 instanceof JSONArray) { if (!(value2 instanceof JSONArray)) return false; if (!equals((JSONArray) value1, (JSONArray) value2)) return false; } else if (!value1.equals(value2)) return false; } } catch (final Exception unused) { unused.printStackTrace(); return false; } return true; }
From source file:hoot.services.controllers.job.JobResource.java
private static Map<String, String> paramsToMap(JSONObject command) { JSONArray paramsList = (JSONArray) command.get("params"); Map<String, String> paramsMap = new HashMap<>(); for (Object aParamsList : paramsList) { JSONObject o = (JSONObject) aParamsList; for (Object o1 : o.entrySet()) { Map.Entry<Object, Object> mEntry = (Map.Entry<Object, Object>) o1; String key = (String) mEntry.getKey(); String val = (String) mEntry.getValue(); paramsMap.put(key, val); }//from w w w . j a va 2 s. c o m } return paramsMap; }
From source file:com.conwet.silbops.model.Subscription.java
/** * Deserializes a Subscription from its JSON representation. * * @param json JSON representation of a subscription * @return A new subscription//from w w w .ja va2 s.co m */ @SuppressWarnings("unchecked") public static Subscription fromJSON(JSONObject json) { Subscription subscription = new Subscription(); subscription.setId((String) json.get("id")); subscription.setContextFunction(ContextFunction.fromJSON((JSONObject) json.get("contextFunction"))); JSONObject constr = (JSONObject) json.get("constraints"); for (Entry<String, JSONArray> entry : (Set<Entry<String, JSONArray>>) constr.entrySet()) { for (JSONObject jsonConstr : (List<JSONObject>) entry.getValue()) { subscription.addConstraint(Attribute.fromJSON(entry.getKey()), Constraint.fromJSON(jsonConstr)); } } return subscription; }
From source file:com.nubits.nubot.options.OptionsJSON.java
public static JSONObject parseFiles(ArrayList<String> filePaths) { JSONObject optionsObject = new JSONObject(); Map setMap = new HashMap(); for (int i = 0; i < filePaths.size(); i++) { try {/* ww w . j a va 2s . co m*/ JSONParser parser = new JSONParser(); JSONObject fileJSON = (JSONObject) (parser.parse(FileSystem.readFromFile(filePaths.get(i)))); JSONObject tempOptions = (JSONObject) fileJSON.get("options"); Set tempSet = tempOptions.entrySet(); for (Object o : tempSet) { Map.Entry entry = (Map.Entry) o; setMap.put(entry.getKey(), entry.getValue()); } } catch (ParseException ex) { LOG.severe("Parse exception \n" + ex.toString()); System.exit(0); } } JSONObject content = new JSONObject(setMap); optionsObject.put("options", content); return optionsObject; }