Example usage for org.json.simple JSONObject keySet

List of usage examples for org.json.simple JSONObject keySet

Introduction

In this page you can find the example usage for org.json.simple JSONObject keySet.

Prototype

Set<K> keySet();

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:com.mycompany.craftdemo.utility.java

public static HashSet<String> getHolidays() {
    HashSet<String> holidays = new HashSet<>();
    JSONObject holidaysData = getAPIData("http://holidayapi.com/v1/holidays?country=US&year=2016");
    JSONObject dates = (JSONObject) holidaysData.get("holidays");

    for (Iterator iterator = dates.keySet().iterator(); iterator.hasNext();) {
        String key = (String) iterator.next();
        holidays.add(key);//from   w ww  .  j a v a  2s .  c  om
    }
    //returning holidays as a set
    return holidays;
}

From source file:net.amigocraft.mpt.command.UpdateCommand.java

@SuppressWarnings("unchecked")
public static void updateStore() throws MPTException {
    if (Thread.currentThread().getId() == Main.mainThreadId)
        throw new MPTException(ERROR_COLOR + "Package store may not be updated from the main thread!");
    lockStores();/* ww w  .  j a v a2s  .c om*/
    final File rStoreFile = new File(Main.plugin.getDataFolder(), "repositories.json");
    if (!rStoreFile.exists())
        Main.initializeRepoStore(rStoreFile); // gotta initialize it before using it
    final File pStoreFile = new File(Main.plugin.getDataFolder(), "packages.json");
    if (!pStoreFile.exists())
        Main.initializePackageStore(pStoreFile);
    JSONObject repos = (JSONObject) Main.repoStore.get("repositories");
    Set<Map.Entry> entries = repos.entrySet();
    JSONObject localPackages = (JSONObject) Main.packageStore.get("packages");
    List<Object> remove = new ArrayList<Object>();
    for (Object k : localPackages.keySet()) {
        if (!((JSONObject) localPackages.get(k)).containsKey("installed"))
            remove.add(k);
    }
    for (Object r : remove)
        localPackages.remove(r);
    for (Map.Entry<String, JSONObject> e : entries) {
        final String id = e.getKey().toLowerCase();
        JSONObject repo = e.getValue();
        final String url = repo.get("url").toString();
        if (VERBOSE)
            Main.log.info("Updating repository \"" + id + "\"");
        JSONObject json = MiscUtil.getRemoteIndex(url);
        String repoId = json.get("id").toString();
        JSONObject packages = (JSONObject) json.get("packages");
        Set<Map.Entry> pEntries = packages.entrySet();
        for (Map.Entry en : pEntries) {
            String packId = en.getKey().toString().toLowerCase();
            JSONObject o = (JSONObject) en.getValue();
            if (o.containsKey("name") && o.containsKey("version") && o.containsKey("url")) {
                if (o.containsKey("sha1") || !Config.ENFORCE_CHECKSUM) {
                    String name = o.get("name").toString();
                    String desc = o.containsKey("description") ? o.get("description").toString() : "";
                    String version = o.get("version").toString();
                    String contentUrl = o.get("url").toString();
                    String sha1 = o.containsKey("sha1") ? o.get("sha1").toString() : "";
                    if (VERBOSE)
                        Main.log.info("Fetching package \"" + packId + "\"");
                    String installed = localPackages.containsKey(packId)
                            && ((JSONObject) localPackages.get(packId)).containsKey("installed")
                                    ? (((JSONObject) localPackages.get(packId)).get("installed")).toString()
                                    : "";
                    JSONArray files = localPackages.containsKey(packId)
                            && ((JSONObject) localPackages.get(packId)).containsKey("files")
                                    ? ((JSONArray) ((JSONObject) localPackages.get(packId)).get("files"))
                                    : null;
                    JSONObject pObj = new JSONObject();
                    pObj.put("repo", repoId);
                    pObj.put("name", name);
                    if (!desc.isEmpty())
                        pObj.put("description", desc);
                    pObj.put("version", version);
                    pObj.put("url", contentUrl);
                    if (!sha1.isEmpty())
                        pObj.put("sha1", sha1);
                    if (!installed.isEmpty())
                        pObj.put("installed", installed);
                    if (files != null)
                        pObj.put("files", files);
                    localPackages.put(packId, pObj);
                } else if (VERBOSE)
                    Main.log.warning("Missing checksum for package \"" + packId + ".\" Ignoring package...");
            } else if (VERBOSE)
                Main.log.warning(
                        "Found invalid package definition \"" + packId + "\" in repository \"" + repoId + "\"");
        }
    }
    Main.packageStore.put("packages", localPackages);
    try {
        writePackageStore();
    } catch (IOException ex) {
        throw new MPTException(ERROR_COLOR + "Failed to save repository store to disk!");
    }
    unlockStores();
}

From source file:at.ac.tuwien.dsg.rSybl.planningEngine.celar.staticData.ActionEffectsCELAR.java

public static HashMap<String, List<ActionEffect>> getActionEffects() {
    HashMap<String, List<ActionEffect>> actionEffects = new HashMap<String, List<ActionEffect>>();

    JSONParser parser = new JSONParser();

    try {/*from  w w  w .j a  v a2s. c om*/
        InputStream inputStream = Configuration.class.getClassLoader()
                .getResourceAsStream(Configuration.getEffectsPath());
        Object obj = parser.parse(new InputStreamReader(inputStream));

        JSONObject jsonObject = (JSONObject) obj;

        for (Object actionName : jsonObject.keySet()) {
            ActionEffect actionEffect = new ActionEffect();
            String myaction = (String) actionName;
            actionEffect.setActionType((String) actionName);
            actionEffect.setActionName(myaction);
            JSONObject object = (JSONObject) jsonObject.get(myaction);
            for (Object actions : object.keySet()) {

                JSONObject scaleinDescription = (JSONObject) object.get(actions);
                String targetUnit = (String) scaleinDescription.get("targetUnit");
                actionEffect.setTargetedEntityID(targetUnit);

                JSONObject effects = (JSONObject) scaleinDescription.get("effects");

                for (Object effectPerUnit : effects.keySet()) {
                    //System.out.println(effects.toString());
                    String affectedUnit = (String) effectPerUnit;
                    JSONObject metriceffects = (JSONObject) effects.get(affectedUnit);
                    for (Object metric : metriceffects.keySet()) {
                        String metricName = (String) metric;
                        try {
                            actionEffect.setActionEffectForMetric(metricName,
                                    (Double) metriceffects.get(metricName), affectedUnit);

                            //System.out.println("metricName="+metricName+" metric effect="+metriceffects.get(metricName)+"Affected unit="+affectedUnit);

                        } catch (Exception e) {

                            actionEffect.setActionEffectForMetric(metricName,
                                    ((Long) metriceffects.get(metricName) + 0.0), affectedUnit);

                            //   System.out.println("metricName="+metricName+" metric effect="+metriceffects.get(metricName)+"Affected unit="+affectedUnit);

                        }
                    }
                }
            }
            List<ActionEffect> l = new ArrayList<ActionEffect>();
            l.add(actionEffect);
            actionEffects.put(actionEffect.getTargetedEntityID(), l);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return actionEffects;
}

From source file:at.ac.tuwien.dsg.rSybl.planningEngine.staticData.ActionEffectsCassandraCluster.java

public static HashMap<String, List<ActionEffect>> getActionEffects() {
    if (actionEffects.isEmpty()) {
        actionEffects = new HashMap<String, List<ActionEffect>>();

        JSONParser parser = new JSONParser();

        try {/* w w  w  . jav a2s . co m*/
            InputStream inputStream = Configuration.class.getClassLoader()
                    .getResourceAsStream(Configuration.getEffectsPath());
            Object obj = parser.parse(new InputStreamReader(inputStream));

            JSONObject jsonObject = (JSONObject) obj;

            for (Object actionName : jsonObject.keySet()) {
                ActionEffect actionEffect = new ActionEffect();
                String myaction = (String) actionName;
                actionEffect.setActionType((String) actionName);
                actionEffect.setActionName(myaction);
                JSONObject object = (JSONObject) jsonObject.get(myaction);
                List<ActionEffect> l = new ArrayList<ActionEffect>();
                for (Object actions : object.keySet()) {

                    JSONObject scaleinDescription = (JSONObject) object.get(actions);
                    String targetUnit = (String) scaleinDescription.get("targetUnit");
                    actionEffect.setTargetedEntityID(targetUnit);

                    JSONObject effects = (JSONObject) scaleinDescription.get("effects");

                    for (Object effectPerUnit : effects.keySet()) {
                        //System.out.println(effects.toString());
                        String affectedUnit = (String) effectPerUnit;
                        JSONObject metriceffects = (JSONObject) effects.get(affectedUnit);
                        for (Object metric : metriceffects.keySet()) {
                            String metricName = (String) metric;
                            try {
                                actionEffect.setActionEffectForMetric(metricName,
                                        (Double) metriceffects.get(metricName), affectedUnit);

                                //System.out.println("metricName="+metricName+" metric effect="+metriceffects.get(metricName)+"Affected unit="+affectedUnit);

                            } catch (Exception e) {

                                actionEffect.setActionEffectForMetric(metricName,
                                        ((Long) metriceffects.get(metricName) + 0.0), affectedUnit);

                                //   System.out.println("metricName="+metricName+" metric effect="+metriceffects.get(metricName)+"Affected unit="+affectedUnit);

                            }
                        }
                    }
                }

                l.add(actionEffect);
                actionEffects.put(actionEffect.getTargetedEntityID(), l);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return actionEffects;
}

From source file:com.nubits.nubot.options.ParseOptions.java

public static Object getIgnoreCase(JSONObject jobj, String key) {

    Iterator<String> iter = jobj.keySet().iterator();
    while (iter.hasNext()) {
        String key1 = iter.next();
        if (key1.equalsIgnoreCase(key)) {
            return jobj.get(key1);
        }/* www.ja  v a2 s  .co  m*/
    }

    return null;

}

From source file:com.nubits.nubot.options.ParseOptions.java

public static boolean containsIgnoreCase(JSONObject jobj, String key) {

    Iterator<String> iter = jobj.keySet().iterator();
    boolean contains = false;
    while (iter.hasNext()) {
        String key1 = iter.next();
        if (key1.equalsIgnoreCase(key))
            return true;
    }/*  w  w w  . j a v a 2 s  .  c o m*/

    return contains;

}

From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java

/**
 * List of APIs under apiConfig/*from ww  w .  ja v  a2 s.  co m*/
 */
public static Set<String> getAPIList(File configFile) throws ParseException, IOException {

    Logger logger = LoggerFactory.getLogger(ConfigReader.class);

    JSONParser parser = new JSONParser();
    ArrayList<String> out = null;
    try {
        BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile));

        JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader);
        if (edgeConf == null)
            return null;

        JSONObject scopeConf = (JSONObject) edgeConf.get("apiConfig");
        if (scopeConf == null)
            return null;

        return scopeConf.keySet();

        // while( keys.hasNext() ) {
        //     out.add((String)keys.next());
        // }
    } catch (IOException ie) {
        logger.info(ie.getMessage());
        throw ie;
    } catch (ParseException pe) {
        logger.info(pe.getMessage());
        throw pe;
    }
}

From source file:bigtweet.model.SpreadModel.java

/**
 * Read the list of models in modelParameters
 *
 * @return/*from   w w w.ja  v  a2  s .  com*/
 */
public static List<String> getListOfModels() {
    JSONObject jo = (JSONObject) SpreadModel.getModelConfiguration().get("spreadModels");
    List<String> l = new ArrayList();
    for (Object o : jo.keySet()) {
        l.add((String) o);
    }
    Collections.sort(l);
    return l;
}

From source file:com.bigdata.dastor.tools.SSTableImport.java

/**
 * Convert a JSON formatted file to an SSTable.
 * /*  ww  w. jav a  2s  .co  m*/
 * @param jsonFile the file containing JSON formatted data
 * @param keyspace keyspace the data belongs to
 * @param cf column family the data belongs to
 * @param ssTablePath file to write the SSTable to
 * @throws IOException for errors reading/writing input/output
 * @throws ParseException for errors encountered parsing JSON input
 */
public static void importJson(String jsonFile, String keyspace, String cf, String ssTablePath)
        throws IOException, ParseException {
    ColumnFamily cfamily = ColumnFamily.create(keyspace, cf);
    String cfType = cfamily.type(); // Super or Standard
    IPartitioner<?> partitioner = DatabaseDescriptor.getPartitioner();
    DataOutputBuffer headerBuffer = new DataOutputBuffer(); // BIGDATA
    DataOutputBuffer dob = new DataOutputBuffer();

    try {
        JSONObject json = (JSONObject) JSONValue.parseWithException(new FileReader(jsonFile));

        SSTableWriter writer = new SSTableWriter(ssTablePath, json.size(), partitioner);
        List<DecoratedKey<?>> decoratedKeys = new ArrayList<DecoratedKey<?>>();

        for (String key : (Set<String>) json.keySet())
            decoratedKeys.add(partitioner.decorateKey(key));
        Collections.sort(decoratedKeys);

        for (DecoratedKey<?> rowKey : decoratedKeys) {
            if (cfType.equals("Super"))
                addToSuperCF((JSONObject) json.get(rowKey.key), cfamily);
            else
                addToStandardCF((JSONArray) json.get(rowKey.key), cfamily);

            ColumnFamily.serializer().serializeWithIndexes(cfamily, headerBuffer, dob,
                    DatabaseDescriptor.getCompressAlgo(keyspace, cf)); // BIGDATA
            writer.append(rowKey, headerBuffer, dob);
            headerBuffer.reset(); // BIGDATA
            dob.reset();
            cfamily.clear();
        }

        writer.closeAndOpenReader();
    } catch (ClassCastException cce) {
        throw new RuntimeException("Invalid JSON input, or incorrect column family.", cce);
    }
}

From source file:com.orthancserver.DicomDecoder.java

private static void ExtractDicomInfo(ImagePlus image, JSONObject tags) {
    String info = new String();

    ArrayList<String> tagsIndex = new ArrayList<String>();
    for (Object tag : tags.keySet()) {
        tagsIndex.add((String) tag);
    }//  w  w w.j a v a 2 s . c o m

    Collections.sort(tagsIndex);
    for (String tag : tagsIndex) {
        JSONObject value = (JSONObject) tags.get(tag);

        if (((String) value.get("Type")).equals("String")) {
            info += (tag + " " + (String) value.get("Name") + ": " + (String) value.get("Value") + "\n");
        }
    }

    image.setProperty("Info", info);
}